[RESOLVED] Detect change to selection in textbox
Hi,
I want to be able to know when the selection of text in my textbox changes so that I can enable or disable the copy button.
Basically, when the event fires, I want to do a check to see if the selected text does not equal nothing so that I can enable the copy button so the selected text can be copied.
Thank you! I hope there's a way of doing this. I went through the events of the textbox and couldn't see one that handles the selection change of a textbox.
P.S. I do not want to have to work from mouse events, such as clicks. This would work by seeing if the selection doesn't equal nothing after clicks in the box, but this wouldn't handle any other way of selection - say selection from my code itself, or using the keyboard to select text without a mouse.
Re: Detect change to selection in textbox
I think it is the textchanged event
Re: Detect change to selection in textbox
ah ...you mean ...when you highlight the text?
Re: Detect change to selection in textbox
Should be able to just record the selected area and compare it.
VB Code:
Private LastSel As String
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If Not LastSel = TextBox2.SelectedText Then
'its changed
End If
LastSel = TextBox2.SelectedText
End Sub
however you would probably put that under a timer instead of of textchanged, same idea though.
Re: Detect change to selection in textbox
The only two ways to select text within a TextBox are with the the mouse or with the keyboard, so you may well be able to use the MouseUp and KeyPress events to initiate a check of the SelectionLength property. If SelectionLength is greater than zero then you would enable the Copy button. You would also have to take into account that the SelectionLength property does not change when the TextBox receives and loses focus, so you may have to change the state of the Button in the GotFocus and LostFocus event handlers as well.
Frankly, I wouldn't bother. If the user wants to click the Copy button when no text is selected then it does no harm because an empty string will be copied to the clipboard. If that's not what they want then more fool them for doing it.
Re: Detect change to selection in textbox
Quote:
Originally Posted by jmcilhinney
The only two ways to select text within a TextBox are with the the mouse or with the keyboard, so you may well be able to use the MouseUp and KeyPress events to initiate a check of the SelectionLength property. If SelectionLength is greater than zero then you would enable the Copy button. You would also have to take into account that the SelectionLength property does not change when the TextBox receives and loses focus, so you may have to change the state of the Button in the GotFocus and LostFocus event handlers as well.
Frankly, I wouldn't bother. If the user wants to click the Copy button when no text is selected then it does no harm because an empty string will be copied to the clipboard. If that's not what they want then more fool them for doing it.
Alright, thanks! I'll just put it to the appropiate mouse and keyboard events. This doesn't cover if my code would make a selection, but that doesn't matter as my code isn't going to - and even if it did, I'd just be able to set the button's enabled property then.
You're right about leaving the button enabled, but I'd prefer to just make copy available when something is going to get copied.
Thanks! Think this is pretty much resolved. I'll say if anything goes wrong.