user3358377
user3358377

Reputation: 145

VB 10 how to hundle multiple buttons

I have made a form in VB10 with 50 buttons. How can i manage their visibility with a for loop ??
For example i want to do something like that:

    For i As Integer = 1 To 50    
        Button(i).Visible = False    
    Next

How can i map the current number of the i? I want to avoid write it 50 times.
Thank you in advance for your help.

Upvotes: 1

Views: 86

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39122

Here's how to get the buttons no matter what container they are in, even multiple ones:

    Dim matches() As Control
    For i As Integer = 1 To 50
        matches = Me.Controls.Find("Button" & i, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then
            Dim btn As Button = DirectCast(matches(0), Button)
            btn.Visible = False
        End If
    Next

Upvotes: 1

OneFineDay
OneFineDay

Reputation: 9024

If there names are Button1, Button2, etc then this will work:

For i As Integer = 1 To 50    
    Me.Controls("Button" & i.ToString).Visible = False    
Next

Upvotes: 0

Related Questions