How to hide multiple buttons

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dandalero
    New Member
    • Aug 2009
    • 11

    How to hide multiple buttons

    if i place 20 different buttons on my form, how can i hide them using the for each function or any other function instead of writing the following for each button:
    "button1.visibl e=true"
    .
    .
    .
    .
    "button20.visib le=true"?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Add the buttons to a List(Of Button) when you add the buttons to the form. Then you can loop through them.

    For example,
    Declare a private List(Of Button) member called _buttons. Then, in the code where you're adding the buttons to the form also add them to this list.

    Code:
    Public Class Form1
    
      Private _buttons As List(Of Button)
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _buttons = New List(Of Button)
        For i As Integer = 0 To 19
          Dim btn As New Button
          btn.Height = 20
          btn.Width = 50
          btn.Text = String.Format("{0}", i + 1)
          btn.Name = String.Format("{0}{1}", "btn_", i + 1)
          btn.Location = New Point(0, i * 20 + 5)
          AddHandler btn.Click, AddressOf Button_Click
          _buttons.Add(btn)
          Me.Controls.Add(btn)
        Next
        Me.Height = 25 * 20
      End Sub
    
      Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show(String.Format("{0} {1}", CType(sender, Button).Name, "was clicked"))
      End Sub
    End Class
    -Frinny

    Comment

    Working...