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

Started by
12 comments, last by phil05 19 years, 1 month ago
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.
Advertisement
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.

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.
Hmm... It doesn't do that to me. This is a textbox right? With multiline and scrollbars enabled?
Quote:Original post by Ralphzehunter
Hmm... 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.
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?

Hmm have you tried simply calling AppendText("") that moves the text cursor thingy to the end before appending text...
Quote:Original post by setaglib
Hmm 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.
It didn't do anything.

Current code:
            txtOutput.Select(txtOutput.Text.Length - 1, txtOutput.Text.Length - 1);            txtOutput.AppendText("");            txtOutput.ScrollToCaret();
Quote:Original post by phil05
It 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.

This topic is closed to new replies.

Advertisement