Referencing controls by an index

Hello,

I am attempting to reference controls using an index by creating a list of controls. I have created a VS/VB2010 project with 5 buttons(Button1,Button2, etc). Here is my code:

Public Class Form1

    Dim Buttons As New List(Of Control)() From { _
                Button1, _
                Button2, _
                Button3, _
                Button4
            }
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Buttons(2).BackColor = Color.Red

    End Sub

End Class

I get a run time error: NullReferenceException:{"Object reference not set to an instance of an object."}

Any suggestions on how to "fix" this?

Thanks,
Bartj
bartdspAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ron MalmsteadInformation Services ManagerCommented:
Did you add the buttons to the form first?
..example..
Me.Form1.Controls.Add(Buttons(0))
0
bartdspAuthor Commented:
The buttons already exist. I do NOT want to add the buttons via the .add method.
0
Ron MalmsteadInformation Services ManagerCommented:
...ahhh..

Put a breakpoint on this line...
Buttons(2).BackColor = Color.Red

..then do a right click "buttons" and choose quick watch.

See if you can actually browse the "tree" of the button array object..
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Ron MalmsteadInformation Services ManagerCommented:
Try it like this and tell me what you get...


..using "button" instead of control.

 Dim Buttons As New List(Of Button)() From { _
              Button1, _
              Button2, _
              Button3, _
              Button4
          }

OR,

 Dim buttons As New Button()

..then in the form load..

buttons.controls.add(button1)
buttons.controls.add(button2)
buttons.controls.add(button3)

etc.
0
bartdspAuthor Commented:
For your suggestion #1:

I only get:

Name = Buttons
Count = 4
Type: System.Collections.Generic.List(Of System.Windows.Forms.Control)


For your suggestion #2

The first 3 buttons are no longer visible on my form(the ones I had originally added manually)

Make sense?
0
Ron MalmsteadInformation Services ManagerCommented:
..is this the one you tried secondly?

" Dim Buttons As New List(Of Button)() From { _
              Button1, _
              Button2, _
              Button3, _
              Button4
          }
"

I wonder if it's as simple as this....
> Me.Buttons(2).BackColor = Color.Red

Unfortunately I don't have my dev computer handy to test.
0
bartdspAuthor Commented:
Hi...using your most recent code, I again get the null reference exception.
0
Fernando SotoRetiredCommented:
Hi bartdsp;

Sounds like you may have declare the Buttons List at class level. If you did this is the cause of your issue. The reason is when the Form is created it first initializes the class variables but the buttons have not yet been created.
0
Fernando SotoRetiredCommented:
Hi bartdsp;

Something like this should correct the issue.    

    Dim Buttons As New List(Of Control)()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Buttons.AddRange({ _
            Button1, _
            Button2, _
            Button3, _
            Button4 _
        })
    End Sub
            }
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Buttons(2).BackColor = Color.Red

    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bartdspAuthor Commented:
Works perfectly!
0
Fernando SotoRetiredCommented:
Great, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.