-
Feb 9th, 2018, 09:10 AM
#1
Thread Starter
New Member
[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
-
Feb 9th, 2018, 09:23 AM
#2
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.
-
Feb 9th, 2018, 09:24 AM
#3
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
-
Feb 9th, 2018, 12:48 PM
#4
Re: Handler Question
Originally Posted by techgnome
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.
-
Feb 9th, 2018, 12:52 PM
#5
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
-
Feb 9th, 2018, 02:13 PM
#6
Thread Starter
New Member
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.
-
Feb 9th, 2018, 02:15 PM
#7
Thread Starter
New Member
Re: [RESOLVED] Handler Question
Oh .. kpmc ... the textboxes exist across several panels so group boxes, while more elegant, isn't so simple. Thanks.
-
Feb 9th, 2018, 02:28 PM
#8
Re: [RESOLVED] Handler Question
Originally Posted by KenB
Oh .. kpmc ... the textboxes exist across several panels so group boxes, while more elegant, isn't so simple. Thanks.
panels have control collections also
-
Feb 9th, 2018, 02:37 PM
#9
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
-
Feb 9th, 2018, 02:44 PM
#10
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.
-
Feb 9th, 2018, 03:20 PM
#11
Re: [RESOLVED] Handler Question
Originally Posted by passel
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...
-
Feb 9th, 2018, 03:41 PM
#12
Thread Starter
New Member
Re: [RESOLVED] Handler Question
Originally Posted by Shaggy Hiker
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
-
Feb 9th, 2018, 08:44 PM
#13
Re: Handler Question
Originally Posted by KenB
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.
Originally Posted by KenB
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|