• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement