Q: How to Work with Control Arrays?

  • Thread starter Thread starter Mr. B
  • Start date Start date
M

Mr. B

I've an app with a bunch of Control Array of Buttons. I'll use code from my
App using 5 buttons as my example:

1) I've Dim them as a button in my Form

Dim btnUsrMonday() As Button ' User Monday Buttons


2) I've the Buttons on the Form named:
btnUsrMon1, btnUsrMon2, btnUsrMon3, btnUsrMon4, btnUsrMon5


3) I then create the Buttons Control Array in my Form

btnUsrMonday = New Button() {btnUsrMon1, btnUsrMon2, btnUsrMon3,
btnUsrMon4, btnUsrMon5}


4) Now I can control them all simply enough. Like the following will change
them all to Red with the text 'Out':

Dim i as Integer
For i = 0 To 4
btnUsrMonday(i).BackColor = Color.Red
btnUsrMonday(i).Text = "Out"
Next

What I'm looking for is a simple way to do something with ONE of them at a
time. For example, if I click on ONE of the buttons, how do I have a
sub-routine for the 'whole' array, find out which one was clicked on (ie:
btnUserMon2) and then do my colour/text change?

Obvious I can do this for each and every button. But there must be a way to
write something for the Control Array so that I can change their colour/text
when only ONE is checked (and then another, another, etc.).

I've seen a sample for Checkboxes that uses COLLECTIONS to do this... but I
can't figour out how to make it work for Buttons.

Appreciate any help.

Regards,

Bruce
 
Mr. B said:
I've an app with a bunch of Control Array of Buttons. I'll use code
from my App using 5 buttons as my example:

1) I've Dim them as a button in my Form

Dim btnUsrMonday() As Button ' User Monday Buttons


2) I've the Buttons on the Form named:
btnUsrMon1, btnUsrMon2, btnUsrMon3, btnUsrMon4, btnUsrMon5


3) I then create the Buttons Control Array in my Form

btnUsrMonday = New Button() {btnUsrMon1, btnUsrMon2,
btnUsrMon3,
btnUsrMon4,
btnUsrMon5}


4) Now I can control them all simply enough. Like the following will
change them all to Red with the text 'Out':

Dim i as Integer
For i = 0 To 4
btnUsrMonday(i).BackColor = Color.Red
btnUsrMonday(i).Text = "Out"
Next

What I'm looking for is a simple way to do something with ONE of them
at a time. For example, if I click on ONE of the buttons, how do I
have a sub-routine for the 'whole' array, find out which one was
clicked on (ie: btnUserMon2) and then do my colour/text change?

Obvious I can do this for each and every button. But there must be a
way to write something for the Control Array so that I can change
their colour/text when only ONE is checked (and then another,
another, etc.).

I've seen a sample for Checkboxes that uses COLLECTIONS to do this...
but I can't figour out how to make it work for Buttons.


You can use the same event handler for different objects:

sub ButtonClick(...) _
Handles btnUsrMon1.click, btnUserMon2.click, ...

dim btn as button = directcast(sender, button)
btn.backcolor = ...
btn.text = ...

end sub

- OR -

dim btn as button
for each btn in btnUsrMonday
addhandler btn.click, addressof ButtonClick
next btn

In this case, you don't need the Handles keyword.
 
Armin Zingler said:
You can use the same event handler for different objects:
dim btn as button
for each btn in btnUsrMonday
addhandler btn.click, addressof ButtonClick
next btn

I'll look at this one as I actually have 19 buttons... so that's a bit more
than 5 (:

Thanks!

Bruce
 
Gary said:
You could make the click events of all the buttons share the same procedure.
Then use the sender parameter
Something like this: (watch for word wrap)

Okay.... I'll even try this (in reality, I've 19 buttons... not 5). So the
code might be long. But then, it's better than doing this for 19 buttons
individually.

Thanks!

Bruce
 
Back
Top