Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I monitor the winforms TextBox.SelectionStart property for changes?

I have a multiline text box, and I'd like to display the user's current line number and column number in a label below the text box. It looks like I can get the line number and column number using the SelectionStart property, GetLineFromCharIndex method, and GetFirstCharIndexOfCurrentLine method. However, I can't see any event that gets fired every time the selection changes in the text box.

The best I can see is to monitor the KeyPress and MouseDown events and check the SelectionStart property after each event. I suppose I could use a timer to watch for changes, but that feels weird.

Have I missed something?

like image 814
Don Kirkby Avatar asked Sep 18 '25 19:09

Don Kirkby


1 Answers

To be robust, you're probably going to have to add a timer, and just check it regularly; from here, the control doesn't post suitable win32 messages (EN_SELCHANGE) to do this as an event.

like image 64
Marc Gravell Avatar answered Sep 20 '25 11:09

Marc Gravell