|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
newbie syntax question: adding event handler for button not workingwhen 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 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 Show quoteHide quote "Keith R" wrote: > 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 > > > 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 Show quoteHide quote "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message news:DFDAFBA5-2120-483D-A05A-C85642698B33@microsoft.com... > 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 > > > "Keith R" wrote: > >> 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 >> >> >> 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 Show quoteHide quote "Keith R" <ASpamfilterAddress@NoMail.org> wrote in message news:e4RwCwpEHHA.4024@TK2MSFTNGP04.phx.gbl... > 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 > > > "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message > news:DFDAFBA5-2120-483D-A05A-C85642698B33@microsoft.com... >> 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 >> >> >> "Keith R" wrote: >> >>> 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 >>> >>> >>> > >
word automation vb.net
Arranging window screens on multi-monitor Desktop Re: Help needed in using FSO's, TextStreams, etc. --- Code Review and Advice requested listbox's SelectedIndexChanged keep fire better way to program than this? set refresh rate for an active browser window How broad should an interface be? Advice needed. File and Database Search Re: Is VB.NET Stable?? Error on deleting registry value |
|||||||||||||||||||||||