If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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?

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.

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.

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.

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

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.

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.

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.

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.

Re: Handler Question

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.