Keplah
Keplah

Reputation: 994

Scroll to the end of a Single-Line Textbox in C#

In regards to single-line textboxes (Multiline property is set to false), is it possible to scroll to the end of the line when the text length exceeds the horizontal size of the box?

I have tried various solutions that work for multi-line boxes, but none of them have worked thus far.

Very similar questions have been asked by several individuals in the past, but it has always regarded Multi-Line textboxes. The questions/solutions that I have come across on SO are the following:

Scroll to bottom of C# TextBox

How do I automatically scroll to the bottom of a multiline text box?

Right now I have the following code (which seemingly does not work):

PathText.Text = "";
PathText.AppendText(BrowseDialog.SelectedPath);
PathText.SelectionStart = PathText.TextLength;
PathText.ScrollToCaret();
PathText.Refresh();

PathText is the textbox in use, and BrowseDialog is a FileDialog.

Any suggestions are greatly appreciated.

Upvotes: 4

Views: 3324

Answers (2)

Damith
Damith

Reputation: 63065

textBox1.Select(textBox1.Text.Length, 0);
// call focus 
textBox1.Focus();

OR

textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Focus();

Upvotes: 1

vcsjones
vcsjones

Reputation: 141638

You could do something like this:

 PathText.Focus();
 PathText.Select(PathText.Text.Length, 0);

Upvotes: 4

Related Questions