Link to home
Start Free TrialLog in
Avatar of bartdsp
bartdspFlag for United States of America

asked on

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
Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

Did you add the buttons to the form first?
..example..
Me.Form1.Controls.Add(Buttons(0))
Avatar of bartdsp

ASKER

The buttons already exist. I do NOT want to add the buttons via the .add method.
...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..
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.
Avatar of bartdsp

ASKER

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?
..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.
Avatar of bartdsp

ASKER

Hi...using your most recent code, I again get the null reference exception.
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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bartdsp

ASKER

Works perfectly!
Great, glad I was able to help.