[RESOLVED] Handler Question-VBForums
Results 1 to 13 of 13

Thread: [RESOLVED] Handler Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    12

    Resolved [RESOLVED] Handler Question

    Using VB2012 ...

    Rather than have a single subroutine for each textbox control getting focus on a form, I grouped them all like this:
    Code:
    Private Sub txtAddress_GotFocus(sender As Object, e As EventArgs) Handles _
    txtAddress.GotFocus, txtPhone.GotFocus, txtVendor.GotFocus, _
    txtCity.GotFocus, txtZip.GotFocus
           
        End Sub
    From there I can do various things like change the backcolor/forecolor but considering they are all textboxes I would like to select all the text when they get focus ... but I can't figure out the syntax to do that. I would think I have to test for a textbox and then select all if true ... but how?

    Thanks,
    Ken

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,637

    Re: Handler Question

    Firstly, as the documentation says, an application developer should not be handling the GotFocus event. Enter is the event you should be handling.

    As for your question, the 'sender' parameter is the object that raised the event so cast it as type TextBox and then call its SelectAll method, which is, as the documentation describes, the method that selects all the text in the TextBox.

    You should always read the relevant documentation first. VS has a Help menu for a reason. You'll often find what you need and lots that you didn't expect that way.
    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,452

    Re: Handler Question

    The sender object that is passed in is the textbox that triggered the event... sooooo... you just need to cast it as a textbox, then use the appropriate methods to select the text and highlight it. - hint - SelectionStart and SelectionLength are two properties you'll need.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    303

    Re: Handler Question

    Quote Originally Posted by techgnome View Post
    The sender object that is passed in is the textbox that triggered the event... sooooo... you just need to cast it as a textbox, then use the appropriate methods to select the text and highlight it. - hint - SelectionStart and SelectionLength are two properties you'll need.

    -tg
    I've discovered this wont do what he wants on the enter event. It's as if the event of entering is clearing the selection, or at least is the case in VS 2017 Ent. It will work on a click event.

  5. #5
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    303

    Re: Handler Question

    I will throw you a bone also. Instead of adding handlers in that way you could do it a bit more dynamically and put your tb's into a groupbox and use the controls collections to find your textboxs and use addhandler to apply a sub to an event. This would work on any container with a controls collections. IE

    Code:
        Private Sub FMLHoursWorked_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each ctrl As Control In GroupBox1.Controls
                If TypeOf ctrl Is TextBox Then
                    Dim tb As TextBox = CType(ctrl, TextBox)
                    AddHandler tb.Click, AddressOf SelectAllText
                End If
            Next
        End Sub
    Code:
        Private Sub SelectAllText(sender As Object, e As EventArgs)
            Dim tb As TextBox = CType(sender, TextBox)
    'TODO: Figure out whats going on here
        End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    12

    Re: Handler Question

    I got almost nothing from google searches ... and marginal help from MSDN. However in trial and error I figured out this which allows me to access the methods I wanted:

    Code:
    If TypeOf (sender) Is TextBox Then
         Dim tbox As TextBox = CType(sender, TextBox)
         tbox.SelectAll()
    End If
    It took a lot of searching but I did find where MS said not to use the gotfocus event but rather enter and leave.

    Thanks for your help.
    K
    Last edited by KenB; Feb 9th, 2018 at 02:16 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    12

    Re: [RESOLVED] Handler Question

    Oh .. kpmc ... the textboxes exist across several panels so group boxes, while more elegant, isn't so simple. Thanks.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    303

    Re: [RESOLVED] Handler Question

    Quote Originally Posted by KenB View Post
    Oh .. kpmc ... the textboxes exist across several panels so group boxes, while more elegant, isn't so simple. Thanks.
    panels have control collections also

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,548

    Re: [RESOLVED] Handler Question

    But with multiple panels, I'd do it the way he's doing it, anyways. It's not THAT many.

    By the way, there may be an infinitesimally small advantage to using DirectCast rather than CType for that cast. I have read that it is faster in cases like this, but have never tested it. If there's a difference, it's going to be too small to notice. FAR too small to notice in this case.
    My usual boring signature: Nothing

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,161

    Re: [RESOLVED] Handler Question

    While you can used code to loop through and add handlers, perhaps you don't want all the textboxes on the form.

    In any case, if you hold down the ctrl key and select all the textboxes you want to include in the handles clause, then generate the event handler sub from the event list, it adds all the controls to the handles clause for you so its not like you have type all that text yourself anyway.

  11. #11
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    303

    Re: [RESOLVED] Handler Question

    Quote Originally Posted by passel View Post
    While you can used code to loop through and add handlers, perhaps you don't want all the textboxes on the form.

    In any case, if you hold down the ctrl key and select all the textboxes you want to include in the handles clause, then generate the event handler sub from the event list, it adds all the controls to the handles clause for you so its not like you have type all that text yourself anyway.
    There is certainly a practicality vs laziness ratio to be had here...

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Posts
    12

    Re: [RESOLVED] Handler Question

    Quote Originally Posted by Shaggy Hiker View Post
    But with multiple panels, I'd do it the way he's doing it, anyways. It's not THAT many.

    By the way, there may be an infinitesimally small advantage to using DirectCast rather than CType for that cast. I have read that it is faster in cases like this, but have never tested it. If there's a difference, it's going to be too small to notice. FAR too small to notice in this case.
    In my searching I came across this comparison ... https://www.codeproject.com/Articles...tCast-Vs-CType

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,637

    Re: Handler Question

    Quote Originally Posted by KenB View Post
    I got [...] marginal help from MSDN.
    You're using a TextBox. The documentation for the TextBox class lists all its members, including the SelectAll method. That method is describedthusly:
    Selects all text in the text box.
    That's not marginal help and it's exactly where you should expect it to be. It's really just a matter of whether you're prepared to spend the time to have a reasonable read of that member list. It is quite long but, if you want to do something with a TextBox then you should assume that knowing what a TextBox can do is a prerequisite. In doing so, you may well find other information that will be useful in the future. I long ago lost count of the number of times that happened to me.
    Quote Originally Posted by KenB View Post
    It took a lot of searching but I did find where MS said not to use the gotfocus event but rather enter and leave.
    It's in the documentation for the GotFocus event, so not a lot of searching really required. Of course, you may not have thought you had a reason to read that documentation specifically.

    That just leaves the use of the 'sender' parameter to access the object that raised the event. That's not something that you'd necessarily have known to look for specifically, which is why I provided that information in post #2. That said, many (if not most) write-ups on handling events will mention the 'sender' parameter.
    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

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width