Windows Develop Bookmark and Share   
 index > Windows Forms General > Scrolling in a textbox is highlighting the text
 

Scrolling in a textbox is highlighting the text

I have some very simple code in the text changed event of a textbox.

tbStatus.SelectionStart = tbStatus.TextLength;
tbStatus.ScrollToCaret ();         
tbStatus.Refresh ();
however this seems to keep the text highlighted within the textbox. I was under the impression setting selection start would resolve this. Any ideas anyone?
Voast Dev  Thursday, May 07, 2009 7:51 PM
Use TextBox.AppendText instead. Not only will it let you add text from your ctor with the behaviour you want, it is also more efficient once the text inside the textbox grows bigger. Also, skip the 'tbStatus.Refresh', most Controls including the TextBox are refreshed automatically when their state changes.

/Calle
- Still confused, but on a higher level -
Calle Mellergardh  Thursday, May 07, 2009 8:59 PM
Where is the caret when you call ScrollToCaret(). Sounds like it as at the start of the text, and ScrollToCaret()moves back through the entire string to select everything.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Thursday, May 07, 2009 8:00 PM
I found it only highlights text that was added in the constructor. If I add later the scrolling works perfect. I think that is kind of odd.

Meaning if I do tbStatus.Text += "Application Ready\r\n"; in the constructor that stays highlighted until something else is added.

I resolved this by creating the following function.

private void AddText ( string msg )
      {
         tbStatus.Text += msg + "\r\n";
         tbStatus.SelectionStart = tbStatus.TextLength;
         tbStatus.ScrollToCaret ();
         tbStatus.Refresh ();
      }
This seems to handle things no matter when the text is added.
Voast Dev  Thursday, May 07, 2009 8:05 PM
Use TextBox.AppendText instead. Not only will it let you add text from your ctor with the behaviour you want, it is also more efficient once the text inside the textbox grows bigger. Also, skip the 'tbStatus.Refresh', most Controls including the TextBox are refreshed automatically when their state changes.

/Calle
- Still confused, but on a higher level -
Calle Mellergardh  Thursday, May 07, 2009 8:59 PM
Thanks I appreciate the feedback and took your advise.
Voast Dev  Friday, May 08, 2009 1:57 AM

You can use google to search for other answers

Custom Search

More Threads

• Tabs, combobox, and webbrowser interact(simple question) Viusal C# 2005 express
• AccessibleObject.QueryAccessibilityHelp event - need some advice
• Help with event handling, event being handled twice.
• Using DoubleBuffered with Panel
• DataGridView event
• TreeView Scroll Issue
• How to tell a click on which item, which column in a ListViewControl?
• Panels or UserControl
• Checking for system requirements
• Frequently Update Large # Of ListView Items?