BUG: TextBox content is not automatically scrolled to display selected text on receiving focus (820638)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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 "Applies to" section.

MORE INFORMATION

Steps to reproduce the problem

  1. Create a new Microsoft Windows application by using one of the following:
    • Visual Basic 2005
    • Visual Basic .NET
    • Visual C# 2005
    • Visual C# .NET
  2. Drag two BUTTON controls and one TEXTBOX control to Form1.
  3. Right-click Textbox1 and then click Properties. In the Properties window, set the Multiline property to True.
  4. In the Properties window, set the Width of the Textbox to 100 and the Height of the Textbox to 50.
  5. Add the following code to the Form1_Load event:

    Visual Basic Code
          Dim data As String() = {"one", "two", "three", "four", "five", "six", "seven"}
          TextBox1.Lines = data
    Visual C# Code
    string [] data = {"one", "two", "three", "four", "five", "six", "seven"};
    			textBox1.Lines = data;
  6. Add the following code to the Button1_Click event:

    Visual Basic Code
          Console.WriteLine("Text.Length = " & TextBox1.Text.Length)
          ' Select the Text Programatically
          TextBox1.Select(TextBox1.Text.Length / 2, 5)
          Console.WriteLine("current selection: ({0}, {1})", TextBox1.SelectionStart, TextBox1.SelectionLength)
          TextBox1.Focus()
    Visual C# Code
    Console.WriteLine("Text.Length = " + textBox1.Text.Length);
    			textBox1.Select(textBox1.Text.Length / 2, 5);
    			Console.WriteLine("current selection: ({0}, {1})", textBox1.SelectionStart, textBox1.SelectionLength);
    			textBox1.Focus();
  7. On the Debug menu, click Start.
  8. 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.

REFERENCES

For more information about TextBox members, visit the following MSDN Web site:

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformstextboxmemberstopic.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbvs2002sp1sweep KbUIDesign kbControl kbWindowsForms kbCtrl kbbug KB820638 kbAudDeveloper