Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Control Arrays in VB.NET?

22 views
Skip to first unread message

John W. Rugo

unread,
Dec 20, 2001, 11:20:56 AM12/20/01
to
Hi All,

I understand that control arrays are no longer supported in VB.NET; but how
can I handle the requirement for a control array. For instance if I have 30
listview controls that previously in VB6 I could alter their similar
attributes by the following means:

dim x as Long
x = 0

for x = 1 to 30
listview(x).backcolor = vbred
next x

Without a control array I would have to reference each controls properties
one at a time. The above example is changing just one attribute. My
situations that I am in are changing many attributes and at many different
times. This seems ridicules. How is this type of situation to be handled
in VB.net?

Thanks
John.


Scott M.

unread,
Dec 22, 2001, 3:24:53 PM12/22/01
to
Hi John,

I posted a very similar question a week or so ago, but with Radio Buttons as
my object type. I agree 200% that what was handled pretty easily in VB 6 is
now more complicated in .NET. I can't say why controll arrays went away in
.NET (I'm sure there was a good reason) but they are gone.

Here's something that can point you in the right direction:

One of the benefits of control arrays was having a common event handler to
write code in just one place. You can still get that benefit by adding
creating a sub...end sub and add "handles objectName.event,
otherObjectName.event, etc." after the sub declaration like this. Consider
the code below wich works with 3 Radio Buttons and needs to figure out which
was clicked in order to work correctly:

Private Sub CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
Dim rad As RadioButton
rad = CType(sender, RadioButton)

Select Case rad.Tag
Case "1"
Me.BackColor = Color.Green
Case "2"
Me.BackColor = Color.Blue
Case "3"
Me.BackColor = Color.Yellow
End Select

End Sub

One interesting new feature of .NET is that most objects have a "Tag"
property that you can place virtually ANY identifier information you want
into. In my case above, I set each of the radio buttons with a Tag value of
1, 2 or 3. Basically, I used the Tag property as you've used the Index
property in VB 6.

Hope this helps.

-Scott M.

"John W. Rugo" <jwr...@rcn.com> wrote in message
news:uGqDqKXiBHA.2260@tkmsftngp07...

Joachim Fuchs

unread,
Dec 22, 2001, 5:43:05 PM12/22/01
to
Hi John,

create an additional array of the appropriate type (i. e. ListView) and
after instantiating the controls set the references in that array. Once you
have initialized that array, you can access your controls as if they belong
to a "ControlArray".

Something like this:
private MyListviews() as new ListView(20)
...
listviewXY = new ...
listviewUVW = new...
...
MyListviews(0) = listviewXY
MyListviews(1) = listviewUWV
...
for i=...
MyListviews(i)....
next


To share events, add the same event function to all controls instead of
creating one for each control. The "Sender" parameter is a reference to the
control that had fired the event.

Joachim

John W. Rugo

unread,
Dec 26, 2001, 8:20:21 AM12/26/01
to
Thanks for the example. I take the biggest issue with the fact that in
VS.NET we all have to write more code and use more resources to do something
that was, in previous versions, less engaging.


"Scott M." <s-...@snet.net> wrote in message
news:ekeahbyiBHA.1544@tkmsftngp02...

John W. Rugo

unread,
Dec 26, 2001, 8:19:01 AM12/26/01
to
Thanks for the idea

"Joachim Fuchs" <dr.f...@fuechse-online.de> wrote in message
news:#05dnnziBHA.2392@tkmsftngp02...

matthew jones

unread,
Jan 2, 2002, 6:35:59 AM1/2/02
to
Another quick and easy way to get around this problem is
to put all your controls within a panel. Once they're
within a <panel> </panel> construct you can access them in
the same way as you did with control arrays...

dim c as object

for each c in panel.controls
...
next

>.
>

0 new messages