Microsoft KB Archive/820638

= BUG: TextBox content is not automatically scrolled to display selected text on receiving focus =

Article ID: 820638

Article Last Modified on 5/9/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
When you programmatically select the content of a text box, and the selected content resides in the area that you cannot see of the TEXTBOX control, then the text in the text box does not scroll automatically to display the selected text. Also, when you move to the TEXTBOX control by pressing TAB, the selected text does not appear automatically when the text box receives the focus.



WORKAROUND
To work around this problem, you can use the ScrollToCaret method of the TEXTBOX control. This method scrolls the content of the control to the current caret position.

Copy the following code to the textBox1_Enter event:

Visual Basic Code textBox1.ScrollToCaret Visual C# Code textBox1.ScrollToCaret;



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the problem
 Create a new Microsoft Windows application by using one of the following:  Visual Basic 2005 Visual Basic .NET Visual C# 2005 Visual C# .NET </li> Drag two BUTTON controls and one TEXTBOX control to Form1.</li> Right-click Textbox1 and then click Properties. In the Properties window, set the Multiline property to True .</li> In the Properties window, set the Width of the Textbox to 100 and the Height of the Textbox to 50 .</li>  Add the following code to the Form1_Load event:

Visual Basic Code Dim data As String = {&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;} TextBox1.Lines = data Visual C# Code string [] data = {&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;}; textBox1.Lines = data; </li>  Add the following code to the Button1_Click event:

Visual Basic Code Console.WriteLine(&quot;Text.Length = &quot; & TextBox1.Text.Length) ' Select the Text Programatically TextBox1.Select(TextBox1.Text.Length / 2, 5) Console.WriteLine(&quot;current selection: ({0}, {1})&quot;, TextBox1.SelectionStart, TextBox1.SelectionLength) TextBox1.Focus Visual C# Code Console.WriteLine(&quot;Text.Length = &quot; + textBox1.Text.Length); textBox1.Select(textBox1.Text.Length / 2, 5); Console.WriteLine(&quot;current selection: ({0}, {1})&quot;, textBox1.SelectionStart, textBox1.SelectionLength); textBox1.Focus; </li> On the Debug menu, click Start.</li> Click Button1 and then press TAB to move among the controls. Notice that when the focus is on the TEXTBOX control, the text does not automatically scroll to show the selected text.</li></ol>

<div class="references_section">