# how do you make C# textboxes focused always on the last line?

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I type in to the chatbox, but it doesn't scroll to the last line. Instead, it just goes to the top of the chatbox again. The new text cannot be seen this way. I always have to scroll down to see the new text. How do I keep this rich textbox focused on the last line? I looked around the net but had no solutions. Any help is great. Thanks.

##### Share on other sites
Ok, I don't exactly what you are doing, but I think I know what you mean. It sound s like you are programmatically entering the lines in, so when that happens, it stays at the top unless you do this.
I have made this happen by doing this:

Once I entered the text, I selected the very end of the text of it using [your text box name here].Select([textbox].Text.Length - 1, [textbox].Text.Length - 1), and used [your text box name here].ScrollToCaret().

I will get an example in a minute.

##### Share on other sites

txtOutput.Select(txtOutput.Text.Length - 1, txtOutput.Text.Length - 1);txtOutput.ScrollToCaret();

This works a little bit, but it goes way too far down on the first enter. I just want the caret to be at the last line of text each time. Aka chatrooms, AIM or MSN Messenger does this while chatting to people.

##### Share on other sites
Hmm... It doesn't do that to me. This is a textbox right? With multiline and scrollbars enabled?

##### Share on other sites
Quote:
 Original post by RalphzehunterHmm... It doesn't do that to me. This is a textbox right? With multiline and scrollbars enabled?

Rich textbox, and yeah, both of those are enabled. I think what it's doing is starting at the length + the whole length again, causing a big black/empty space to happen below the text.

##### Share on other sites
Here's a screenshot of what I mean... you can barely move this scrollbar, and it goes to the top, and this black space isn't there anymore. Press enter, this big black space comes in again. Can you maybe show your code that you use?

##### Share on other sites
Hmm have you tried simply calling AppendText("") that moves the text cursor thingy to the end before appending text...

##### Share on other sites
Quote:
 Original post by setaglibHmm have you tried simply calling AppendText("") that moves the text cursor thingy to the end before appending text...

That works for me as well, try that out and see if you get different results.

##### Share on other sites
It didn't do anything.

Current code:
            txtOutput.Select(txtOutput.Text.Length - 1, txtOutput.Text.Length - 1);            txtOutput.AppendText("");            txtOutput.ScrollToCaret();

##### Share on other sites
Quote:
 Original post by phil05It didn't do anything. Current code: txtOutput.Select(txtOutput.Text.Length - 1, txtOutput.Text.Length - 1); txtOutput.AppendText(""); txtOutput.ScrollToCaret();

It worked for me when I did not have ScrollToCaret after it, and it did not work when I did have that after that so, you should try it again without ScrollToCaret.

##### Share on other sites
Maybe you are just adding too many lines? For example if you have an array of 10 strings and you are only using 3 the other 7 lines will be left blank if you assign it like that to the Lines property it'll add the empty lines to the textbox. In that case it'll just move to the wrong position regardless of what you call.

##### Share on other sites
I'll play around with that.

I tried the problem on two compilers and the same thing is happening.

Sorry, Ralphzehunter, MSN Messenger kicked me off and I couldn't get back in til 10 minutes later.

##### Share on other sites
Well, I enter one line at a time. The scrollbar's drag button is going up instead of staying down. This prevents me from seeing the new text below. (Would be annoying on AIM/MSN Messenger). Here's my current code on this test. This is the "Submit" button that grabs input of the input textbox and throws it on the output.

void BtnSubmitClick(object sender, System.EventArgs e){     string sBuffer = txtInput.Text;     txtOutput.Text += sBuffer + Environment.NewLine;     txtOutput.Select(txtOutput.Text.Length - 1, txtOutput.Text.Length - 1); }

##### Share on other sites
I got it! After the 3 lines within that code block...

txtOutput.Focus(); // focus on txtOutput first (always grabs last line)
txtInput.Focus(); // focus on the input box for next input

It's a solution, does the job, etc. This is the only way I got it working.