newbie syntax question: adding event handler for button not working

K

Keith R

in my form load, I've added the following (along with the sub below)- but
when I run my test form and click on the buttons, I'm not getting the
msgbox. I added the tag on each button because I'll also have other buttons
on the form that I don't want to have use this event handler. I'm not sure
if my error is in the parameters being passed, or the way I'm referencing
the controls/buttons, or both. Any help would be greatly appreciated!
Keith

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'***********************
'Add FlagButtons common event handler
Dim FlagCtl As Control
Dim FlagBtn As Button
For Each FlagCtl In Me.Controls
If FlagCtl Is FlagBtn Then
If FlagCtl.Tag = "Filter" Then
AddHandler Control.MouseDown, AddressOf Me.mousendownhandler
End If
End If
Next
End Sub

Sub mousendownhandler(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
MsgBox(sender.text)
End Sub
 
G

Guest

Keith,

For Each FlagCtl As Control In Me.Controls
If TypeOf FlagCtl Is Button Then
If FlagCtl.Tag = "Filter" Then
AddHandler FlagCtl.MouseDown, AddressOf
Me.mousendownhandler
End If
End If
Next

Kerry Moorman
 
K

Keith R

Kerry- thank you, I wouldn't have know to do those three changes, but they
make sense to me now that I see them. I still was't successful until I tried
to trace the code manually with breakpoints, and found out that the code
doesn't pick up the fact that most of my buttons are on a panel (the panel
was there in the original movie collection sample app I've been
cannibalizing). I updated to look at that panel and Woot! it works now.
Thank you again for your assistance with the syntax problems.

The working code:

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Have all FlagButtons common event handler (when clicked)
For Each FlagCtl As Control In Me.FlagPanel.Controls
If TypeOf FlagCtl Is Button Then
If FlagCtl.Tag = "Filter" Then
AddHandler FlagCtl.MouseDown, AddressOf Me.mousendownhandler
End If
End If
Next
End Sub

Sub mousendownhandler(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs)
MsgBox(sender.text)
End Sub
 
S

Stephany Young

That's cool, Keith, but there's a little gotcha that I think you might have
overlooked.

You say that 'most of my buttons are on a panel' which implies that you have
some buttons that are not 'on the panel' and your code will miss those
buttons. Now, it may well be that none of the 'missed' buttons have their
Tag's set to "Filter", but can you be absolutely sure of that.

You can make sure that you cater for that situation by a basic recursive
procedure that will 'walk' the 'tree' of controls on your form and make sure
that all buttons are tested, something like this:

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles MyBase.Load

AddHandlerForAllNecessaryButtons(Me.Controls)

End Sub

Private Sub AddHandlerForAllNecessaryButtons(controlcol As
ControlCollection)

'Have all FlagButtons common event handler (when clicked)
For Each FlagCtl As Control In controlcol
If TypeOf FlagCtl Is Button AndAlso FlagCtl.Tag = "Filter" Then
AddHandler FlagCtl.MouseDown, AddressOf Me.mousendownhandler
End If
If FlagCtl.HasChildren Then
AddHandlerForAllNecessaryButtons(FlagCtl.Controls)
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top