Don Kirkby
Don Kirkby

Reputation: 56620

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?

Upvotes: 4

Views: 1377

Answers (2)

bsegraves
bsegraves

Reputation: 1020

To get SelectionChanged events, you can also convert your TextBox to a RichTextBox. RichTextBoxes behave a little differently than TextBoxes so this may not be an ideal solution, but at least there is a SelectionChanged event.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062650

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.

Upvotes: 4

Related Questions