Results 1 to 6 of 6

Thread: [RESOLVED] Detect change to selection in textbox

  1. #1

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    Resolved [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.

  2. #2
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Detect change to selection in textbox

    I think it is the textchanged event

  3. #3
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Detect change to selection in textbox

    ah ...you mean ...when you highlight the text?

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Detect change to selection in textbox

    Should be able to just record the selected area and compare it.

    VB Code:
    1. Private LastSel As String
    2. Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    3.         If Not LastSel = TextBox2.SelectedText Then
    4.             'its changed
    5.         End If
    6.         LastSel = TextBox2.SelectedText
    7. End Sub

    however you would probably put that under a timer instead of of textchanged, same idea though.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width