bartdsp
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:{"O bject reference not set to an instance of an object."}
Any suggestions on how to "fix" this?
Thanks,
Bartj
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:{"O
Any suggestions on how to "fix" this?
Thanks,
Bartj
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..
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(butto n1)
buttons.controls.add(butto n2)
buttons.controls.add(butto n3)
etc.
..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(butto
buttons.controls.add(butto
buttons.controls.add(butto
etc.
ASKER
For your suggestion #1:
I only get:
Name = Buttons
Count = 4
Type: System.Collections.Generic .List(Of System.Windows.Forms.Contr ol)
For your suggestion #2
The first 3 buttons are no longer visible on my form(the ones I had originally added manually)
Make sense?
I only get:
Name = Buttons
Count = 4
Type: System.Collections.Generic
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.
" 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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works perfectly!
Great, glad I was able to help.
..example..
Me.Form1.Controls.Add(Butt