button.tag

leetpg

Member
Joined
Jul 16, 2007
Messages
8
Programming Experience
1-3
Hi all

I have a range of buttons on my form. To each button i have assigned a value to their tag which based on user input will determine if the button will be shown or not. My question is, instead of checking one variable against each button is there a way to check it against ALL buttons in one single process?
 
something like this:

VB.NET:
    Dim mybutton As Windows.Forms.Button
    For Each mybutton In Me.Controls
      If TypeOf (mybutton) Is Button Then
        if mybutton.tag = "XYZ" then
         'do something
        end if
      End If
    Next

you can probably skip the lines checking for the type of the control and go directly to the .tag property.
 
I would keep it a little more generic than that in case he's wanting to use the same loop to check for other controls as well:
VB.NET:
For Each ctrl As Control In Me.Controls
  If TypeOf ctrl Is Button Then
    If Ctype(ctrl, Button).Tag = "XYZ" Then
      'Handle the button
    End If
  End If
Next Ctrl
 
Back
Top