MTT
MTT

Reputation: 49

Is there anyway to save a multiple buttons as a list?

I am making small restaurant system project. I have 16 buttons as tables in the restaurant. I would like to change their colors or disable any of them when some events trigger (the form is loaded).

I save my button names in format TableX_ButtonY

I used a for-loop to change theirs border colors like this:

CType(Me.Controls.Find(String.Format("Table{0}Button{1}", i, x), True)(0), Button).FlatAppearance.BorderColor = Color.Blue

It will be great if I can save these buttons as a list, so I can manage it more easily.

I name their tags from 1 to 16 but I don't know how to use them correctly. Because the trigger not based on button click but rather based on the Load Form event.

Upvotes: 0

Views: 169

Answers (2)

Mr. Tripodi
Mr. Tripodi

Reputation: 827

Buttons are already in a collection and is a bit redundant to add them to a generic collection. In this example there are 2 buttons in a group controls collection, which could very well be any applicable container.

    Dim ReservedTables() As Integer = {5, 10, 15, 20}

    For Each Btn As Button In GroupBox1.Controls.OfType(Of Button)
        If ReservedTables.Contains(CType(Btn.Tag, Integer)) Then
            Btn.Enabled = False
        End If
    Next

Upvotes: 2

AConfusedSimpleton
AConfusedSimpleton

Reputation: 420

 Dim TableList As New List(Of Button) 

 TableList.Add(TableX_ButtonY)


 For Each Table As Button in TableList
     'do stuff
 next

if you generated the buttons by using the designer, you can use the method you described in your question to add them all to the list

Upvotes: 1

Related Questions