4

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.

Community
  • 1
  • 1
Keplah
  • 954
  • 2
  • 13
  • 26
  • 1
    Just a guess: PathText.SelectionStart = PathText.Text.Length; – kol Jun 13 '12 at 17:15
  • You are correct; I initially copied old code that I had not compiled yet when I typed up the question. Thanks for taking a look at it. – Keplah Jun 13 '12 at 17:27

2 Answers2

4

You could do something like this:

 PathText.Focus();
 PathText.Select(PathText.Text.Length, 0);
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • The problem was elsewhere in the code apparently, but I will go ahead and mark this as the answer because .Focus(); is a crucial part of the process based on the previous questions and other discussions I found from the Google searches. Thanks though! – Keplah Jun 13 '12 at 17:23
1
textBox1.Select(textBox1.Text.Length, 0);
// call focus 
textBox1.Focus();

OR

textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Focus();
Damith
  • 62,401
  • 13
  • 102
  • 153