Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Button Array in Visual Basic 2008

Posted on 2008-10-27
7
Medium Priority
?
2,810 Views
Last Modified: 2013-11-26
How to I add buttons to an array.  I have 42 buttons on a form labeled btn1 through btn42.  I want to use a loop to add the buttons to an array.  I can add them one at a time
button(0) = me.btn1
button(1) = me.btn2
button(2) = me.btn3 and so on.

I can use a subscript for the button variable in a loop but how do I increment through the btn1 through btn42?
Once I get the buttons in an array, it will be much easier to write code to deal with the button presses.  Thank you for any information you can give me.
0
Comment
Question by:don_rowlett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Expert Comment

by:mkosbie
ID: 22817988
Use Me.FindControl.
For i=0 To 41
   buttonArr(i) = CType(Me.FindControl("btn" & i), Button)
Next

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22818009
Are you working with WebForms or WinForms?...

Generally it's easier to use multiple Handles clauses or use AddHandler() to wire up the buttons instead of creating an actual array.  Are you coming from VB6?...
0
 

Author Comment

by:don_rowlett
ID: 22818037
I am using VB.net 2008 and winforms.
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 
LVL 48

Expert Comment

by:jpaulino
ID: 22818402
You can use a generics list to add all the buttons as an array.
Dim button As New List(Of Button)
button.Add(Me.Button1)
button.Add(Me.Button2)
button.Add(Me.Button3)
button.Add(Me.Button4)
 
' Then you can use
button(1).Enabled = False ' Disables the button 2

Open in new window

0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 800 total points
ID: 22818416
To use an increment you can do (check snippet)
BUT I have to agree with Idle_Mind. This is VB6 way and .NET offers better handlings.

        For x As Byte = 1 To 42
            Dim ctrl() As Control = Controls.Find("Button" & x, True)
            If ctrl.Length = 1 Then
                Dim btn As Button = DirectCast(ctrl(0), Button)
                btn.Enabled = False
            End If
        Next

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 total points
ID: 22818433
Here is how I would do it with that many buttons:
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim btn As Button
        Dim matches() As Control
        For i As Integer = 1 To 42
            matches = Me.Controls.Find("btn" & i, True)
            If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then
                btn = DirectCast(matches(0), Button)
                AddHandler btn.Click, AddressOf btn_Click
            End If
        Next
    End Sub
 
    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = DirectCast(sender, Button)
        Dim index As Integer = Integer.Parse(btn.Name.Substring(3))
        MessageBox.Show("Click: " & btn.Name & vbCrLf & "Index: " & index)
    End Sub
 
End Class

Open in new window

0
 

Author Closing Comment

by:don_rowlett
ID: 31510576
Thank you very much for your help.  The final solution works great.  I really like the way you added every button to the button click event.
0

Featured Post

Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question