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

  • Thread starter Thread starter Confessor
  • Start date Start date
C

Confessor

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.
 
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
 
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 said:
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 said:
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.
 

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

Back
Top