Control Arrays

P

Pete Boyd

In VB6 it was very easy to add multiple labels to a form and access them as
an array of controls.
How does one do the same thing in VB.NET without falling back to using the
VisualBasic.Compatibility namespace? Its driving me mad!

eg

If blah=x then
label1(0).caption="something"
label1(1).caption="something else"
end if

Thanks
Pete
 
C

Cor

Hi Pete,

If you want it, it can still, I paste below a sample from something that I
did made, but you cannot use the designer than.

If you use the designer you have to remember that a control is always a
child from his parent. So a label on a form is a child from that form.

You can itererate thru it by
\\\
dim ctr as control
for each ctr on me.controls
if typeof ctr = label 'then it is a label
dim ctrlabel as label = directcast(ctr,label) ' and now it is a label
end if
next
///

When it is on a panel it goes like this
for each ctr on me.panel.controls

But to show that it is always posible on another way, this example, but it
is not normal done so.

I hope this helps a little bit?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dim mybutton(31) As Button
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
For i = 0 To System.DateTime.DaysInMonth(2003, 12) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim month As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & month.Text)
End Sub
End Class
///

I hope this helps a little bit?

Cor
 
M

Marc

-----Original Message-----
In VB6 it was very easy to add multiple labels to a form and access them as
an array of controls.
How does one do the same thing in VB.NET without falling back to using the
VisualBasic.Compatibility namespace? Its driving me mad!

eg

If blah=x then
label1(0).caption="something"
label1(1).caption="something else"
end if

Thanks
Pete


.
Try The Following:

Place four checkboxes on a form

Private Sub CheckBoxControlArray(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.Click, CheckBox2.Click,
CheckBox3.Click, CheckBox4.Click

Select Case sender.Name
Case CheckBox1.Name
CheckBox1.Checked = True
CheckBox2.Checked = False
CheckBox3.Checked = False
CheckBox4.Checked = False
Case CheckBox2.Name
CheckBox1.Checked = False
CheckBox2.Checked = True
CheckBox3.Checked = False
CheckBox4.Checked = False
Case CheckBox3.Name
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = True
CheckBox4.Checked = False
Case CheckBox4.Name
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
CheckBox4.Checked = True
End Select
End Sub
 
W

woodpecker

Hi,you can do like this:

Dim _tb As TextBox() = New TextBox() {textbox1,textbox2,textbox3}
For _i = 0 To _tb.GetUpperBound(0)
_tb(_i).Text = ""
Next
_tb(0).text="0"
_tb(1).text="1"
_tb(2).text="2"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top