Events handling multiple controls

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
I have a FormatDate() routine that I call on the LostFocus Event for all my dates. If the entry in the control is an invalid date, the entry will become blank.

Rather than coding each individual date field on Lost Focus, I noticed that you can create one Event Handler that handles all my dates:

EX:
Private Sub Date_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAAdmitDate.LostFocus,txtAAdmitTime.LostFocus, txtALMP.LostFocus, (etc...)
-------------------------------------------------------------------
This works great when I want ALL my comboboxes to have .dropped down on Got Focus(). I'm using the reference Me.ActiveControl:
(Ex: Me.ActiveControl.droppeddown = True) but for LostFocus, the active control is the NEXT control (that gets tabbed into!) so I can't seem to do the above for LostFocus,I've even tried Leave, but it's the same.

How can I get the Event Handler for all my dates to wipe out the date, if it's invalid, and the user goes to another control (either by tab or mouse)?
 
There are a few points to note here.

1. You should not be using the LostFocus event. It is only for VERY sopecifc circumstances as detailed in its help topic. You should be using the Leave, Validating or Validated event, depending on exactly what you want to achieve.

2. The 'sender' argument is ALWAYS the object thatraised the event. It will be a reference to the actual TextBox that is being left, validating or validated. 'sender' is always type Object for the very reason that the same method can handle events for many different objects. You can cast it as the specific type if you require any of the members of that type.

3. You should probably be using DateTimePicker controls for date and time input. That way there is no validation required as the control handles it all for you.
 
By the way, am I using the Handles properly, or is there a better way.

In other words, I'm doing this:

Private Blah_GotFocus(blah blah) Handles blah.GotFocus,blah2.GotFocus

And so on...

is there a way to avoid the Handles blah blah part, because I have a LOT of objects (in this case...combo boxes that I want to dropdown on focus)
 
Code Sample of the AddHandler John spoke of.

In a program I have I want to do the same task everytime the mouse moves accross any control. So this is my code to make that happen, you will notice it's very short and I have 30+ controls.

VB.NET:
Expand Collapse Copy
[SIZE=2][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Setup_TintHandlers([/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]     Dim[/COLOR][/SIZE][SIZE=2] C [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].GetNextControl([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]     Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] C [/SIZE][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]          AddHandler[/COLOR][/SIZE][SIZE=2] C.MouseMove, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Form1_MouseMove
          C = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].GetNextControl(C, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]     Loop
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE]

I just call that Sub to setup the handles for everycontrol. Notice I left out the form itself because I used the original Form1_MouseMove Event sub for the other handlers.
 
dpatfield66 said:
By the way, am I using the Handles properly, or is there a better way.

In other words, I'm doing this:

Private Blah_GotFocus(blah blah) Handles blah.GotFocus,blah2.GotFocus

And so on...

is there a way to avoid the Handles blah blah part, because I have a LOT of objects (in this case...combo boxes that I want to dropdown on focus)

Just add this to your form where you want to make it happens:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]   For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] cmbControl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls
[/SIZE][SIZE=2][COLOR=#0000ff]       If[/COLOR][/SIZE][SIZE=2] cmbControl.GetType [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](ComboBox) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]          AddHandler[/COLOR][/SIZE][SIZE=2] cmbControl.GotFocus, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] GotFocusEvent
[/SIZE][SIZE=2][COLOR=#0000ff]       End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
 
[/COLOR][/SIZE][SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] GotFocusEvent([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)
     sender.DroppedDown = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
This will make that all combo controls that recieve focus to set their DroppedDown property to true
 
Here's a quote from the help topic for the Control.GotFocus event:
The GotFocus and LostFocus events are low-level focus events that are tied to the WM_KILLFOCUS and WM_SETFOCUS Windows messages. Typically, the GotFocus and LostFocus events are only used when updating UICues. The Enter and Leave events should be used for all controls except the Form class, which uses the Activated and Deactivate events. For more information about the GotFocus and LostFocus events, see the WM_SETFOCUS and WM_KILLFOCUS topics in the Keyboard Input Reference section of the Platform SDK Documentation in the MSDN Library.
 
I can't seem to get this code working:

Private Sub frmAdmit_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

For Each cmbControl As Control In Me.Controls
If cmbControl.GetType Is GetType(ComboBox) Then
AddHandler cmbControl.Enter, AddressOf EnterEvent
End If
Next

End Sub

Private Sub EnterEvent(ByVal sender As Object, ByVal e As System.EventArgs)
sender.DroppedDown = True
End Sub

Is it because of the "Handles Me.Load" as opposed to "Handles MyBase.Load"?
 
Well it works and you can see that from the example below but notice that you should not expect it to work if you want it to be set as DroppedDown = True if you change the focus with the mouse ... then you deal with another event but gotFocus. Try with the Tab key.
 

Attachments

I actually need the combo box to drop down whether I use TAB or click into the field with the mouse.

I'm not sure I understand what you're saying. Are you saying that If I use the mouse to enter the field, it will not work?

Also, I used the tab key and it does not work, but I will check your zipped file to see what's going on.
 
I tried to open this and got the following error:

Conversion Issues - GotFocusForCombos.vbproj:MSB2016: Found an empty .RESX file in the project (Form1.resx). Removing it from the converted project.

I'll try this with 2003, I've got both.
 
Back
Top