By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

85 Identical Procedures Are Having An Orgy In My Program. Could You Help?

P: n/a
Having just figured out the function of the *sender* argument in control
events, I now have 85 labelX_Click procedures that perform the exact same
function: Call ColorChange(sender). That's it.

I could make one procedure to handle them all, of the form:

Sub GUITileProc([arguments here]) Handles Label1.Click, Label2.Click... all
the way to Label85.Click.

But that would be really messy, and if there's one thing I hate, it's messy
code. Is there any way that I could group those events together elsewhere,
under a "group" name, then just say that GUITileProc "Handles
GUITileClicks"?

It'd make future code modification that much easier.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You could run a loop through the controls for that form and if the .Name
field matches (use regex or just an instr if you wish) then do an
for each myControl in Controls
if (instr(myControl.Name, "MyLabelName_")) then
AddHandler myControl.Click, New EventHandler(AddressOf GUITileProf)
end if
next

"Confessor" <in*****@reply.to.group> wrote in message
news:Xn*********************************@130.81.64 .196...
Having just figured out the function of the *sender* argument in control
events, I now have 85 labelX_Click procedures that perform the exact same
function: Call ColorChange(sender). That's it.

I could make one procedure to handle them all, of the form:

Sub GUITileProc([arguments here]) Handles Label1.Click, Label2.Click... all the way to Label85.Click.

But that would be really messy, and if there's one thing I hate, it's messy code. Is there any way that I could group those events together elsewhere,
under a "group" name, then just say that GUITileProc "Handles
GUITileClicks"?

It'd make future code modification that much easier.

Nov 21 '05 #2

P: n/a
Or if all the labels on your form (tab page control, user control, etc) are
using the same event handler then you could do:

' pass in your form, tab page, user control, etc as parent
Private Sub AddAllHandlers(Parent As Control)
For Each Ctrl In Parent.Controls
If TypeOf Ctrl Is Label Then
AddHandler Ctrl.Click, AddressOf GUITileProf
End If
Next Ctrl

Imran.

"CJ Taylor" <cege [ahh ttt] tavayn wooohooo hooo ohhh com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
You could run a loop through the controls for that form and if the .Name
field matches (use regex or just an instr if you wish) then do an
for each myControl in Controls
if (instr(myControl.Name, "MyLabelName_")) then
AddHandler myControl.Click, New EventHandler(AddressOf GUITileProf)
end if
next

"Confessor" <in*****@reply.to.group> wrote in message
news:Xn*********************************@130.81.64 .196...
Having just figured out the function of the *sender* argument in control
events, I now have 85 labelX_Click procedures that perform the exact same
function: Call ColorChange(sender). That's it.

I could make one procedure to handle them all, of the form:

Sub GUITileProc([arguments here]) Handles Label1.Click, Label2.Click...

all
the way to Label85.Click.

But that would be really messy, and if there's one thing I hate, it's

messy
code. Is there any way that I could group those events together
elsewhere,
under a "group" name, then just say that GUITileProc "Handles
GUITileClicks"?

It'd make future code modification that much easier.


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.