none
Can you create an array of command buttons with VB Express 2008?

    Question

  • When I used to play wiith VB 5.0 I was able to create an array of comand buttons on the form. This allowed me to have one routine that knew which of many buttonshad been clicked and I could then  take specific action based on which  button was clicked. I can't see how to do this in VBE 8. Is this possible? Thanks

    Saturday, November 21, 2009 9:18 PM

Answers

  • add a handler for each button to the sub, then see which button was clicked

    try this on an app with 3 buttons

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click, Button3.Click
    
            Dim btn As Button = CType(sender, Button)
    
            MessageBox.Show(btn.Name)
    
        End Sub
    • Proposed as answer by Cor LigthertMVP Sunday, November 22, 2009 5:31 PM
    • Marked as answer by Romo9999 Sunday, November 22, 2009 9:14 PM
    Saturday, November 21, 2009 9:37 PM

All replies

  • add a handler for each button to the sub, then see which button was clicked

    try this on an app with 3 buttons

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click, Button3.Click
    
            Dim btn As Button = CType(sender, Button)
    
            MessageBox.Show(btn.Name)
    
        End Sub
    • Proposed as answer by Cor LigthertMVP Sunday, November 22, 2009 5:31 PM
    • Marked as answer by Romo9999 Sunday, November 22, 2009 9:14 PM
    Saturday, November 21, 2009 9:37 PM
  • An array of controls is implemented with a List(Of.

    If you create the buttons at run time, you simply add them to the list as you create them.   If you want to create them at design time, then there are some small complications.

    If the buttons are all placed on the form (not in a container control) and if they can be identified (eg from their name) you can scan the controls collection and place them into a List.

            Dim Buttons As List(Of Button) = New List(Of Button)
            For Each C As Control In Me.Controls
                If TypeOf (C) Is Button Then
                    If C.Name.StartsWith("Button") Then
                        Buttons.Add(C)
                    End If
                End If
            Next
    If the buttons are in a container then you scn the controls collection for the container, not the form.

    That code will place the buttons into the List in the order they appear in the Controls collection - you may need to sort the list for the index to make sense within your applciation.  If you create the buttons at run time then obviously you control the order in which they appear in the list.
    Saturday, November 21, 2009 10:01 PM
  • You can do this with different ways.

    one is

     

    Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,  Butto2.Click, Button2.Click.....
    'your code goes here
    End Sub
    

    one another way is

    For Each MyButton As Button In Me.Controls
    AddHandler MyButton.Click, AddressOf ButtonClick
    Next
    
    Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Your Code Goes Here
        End Sub

     

     

    Sunday, November 22, 2009 7:41 AM
  • Acamar

    just a couple of clarifications:

    with Option Strict On (which we should encourage), you will get an implicit conversion error

    the code will place the Buttons into the List in the REVERSE order they were added. When you loop through the controls using a For Each loop, it starts with the control added to the Form last, and works toward the first.

    to see what I mean try this in an app with 3 buttons - you'll see what I mean. The button at index = 0 in the List is not Button1


    I'll see if I can find the MSDN page that mentions the reverse loop order :)
    Dim Buttons As List(Of Button) = New List(Of Button)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            For Each C As Control In Me.Controls
                If TypeOf (C) Is Button Then
                    If C.Name.StartsWith("Button") Then
                        Buttons.Add(CType(C, Button))
                    End If
                End If
            Next
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Debug.WriteLine(Buttons(0))
        End Sub
    Sunday, November 22, 2009 7:52 AM
  • Hi Romo,

    That you used those arrays was probably because in VB5 the Case Select was the way to go.

    Nowadays programs are more event driven, so every button has a meaning and his own code.

    I can easily give (in endless ways) you the code to let you keep your old codding style, but I think that I don't help you with that.

    Simpy put the code under the event of the button that should do it, then it is better to maintain then those endless Case Select pages you have now in your VB 5.0 program.



    Success
    Cor
    Sunday, November 22, 2009 9:04 AM
  • Start a new Windows Forms Application and replace the code on Form1 with:

    ublic Class Form1
      Dim Btn(99) As Button
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For I As Integer = 0 To 99
          Btn(I) = New Button
          Btn(I).Parent = Me
          Btn(I).Text = "Btn " + I.ToString
          Btn(I).Width = 60
          Btn(I).Left = (I Mod 10) * Btn(0).Width
          Btn(I).Top = (I \ 10) * Btn(0).Height
          Btn(I).BackColor = Color.LimeGreen
          AddHandler Btn(I).Click, AddressOf Btns_Click
        Next
        Me.Width = Btn(99).Right + 6
        Me.Height = Btn(99).Bottom + 28
      End Sub
      Private Sub Btns_Click(ByVal sender As Object, ByVal e As EventArgs)
        Static LastBtn As Button = Btn(0)
        LastBtn.BackColor = Color.LimeGreen
        LastBtn = DirectCast(sender, Button)
        LastBtn.BackColor = Color.Red
      End Sub
    End Class
    Sunday, November 22, 2009 10:50 AM
  • Yes, Sir Cor Lightert is right. now programs are event driven and it is better if control handle its own code.
    we use that old style code "For Each .... Controls ... Add Handler .... Next" --- Statements for runtime created controls only.
    Sunday, November 22, 2009 11:27 AM
  • H,i your idea worked perfectly - it was exactly what I was looking for.

    Thank you!

    Sunday, November 22, 2009 3:05 PM
  • if your question has been answered, please mark the post that answered your question as Answer, and any helpful posts as Helpful (if there were any)
    Sunday, November 22, 2009 3:45 PM
  • jwavila,

    When I was lookin' at this thread first I thought, I propose the question from jwavila as answer especially as I see here given to often that question were handlers are created in that way that is also in C#.

    However, then I realised me that your answer did not really answer the question this is not an array of controls.

    Then I thought, let also make a nice generic list of buttons, but then I realized me that was not helpful for the OP.

    So I created an answer which would help him more on his rourte to VB for Net in my idea comming from VB5

    Don't think that I've a problem as the OP declares your code as answer.

    He does not recognize what I saw and I've no problem with that.



    Success
    Cor
    Sunday, November 22, 2009 5:50 PM
  • Cor

    no problem.

    I'm not sure if the OP really wants an array of Buttons. I took it as the OP just wants to be able to determine which button was clicked, and thought you would have to use an array of Buttons to determine that.

    So I just showed how to determine which button was clicked, without using an array. Then the OP can do whatever is needed once the clicked Button is determined.

    As is so often the case, the question is not real clear.
    Sunday, November 22, 2009 6:08 PM