By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,639 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,639 IT Pros & Developers. It's quick & easy.

Replacement for VB6 control Arrays

P: n/a
I remember when VB.net came out that a couple of the seminars I went to
mentioned that Control Arrays were going away and that there was a new and
better way to execute the same code when an event occurs in one of a group
of similar controls. Unfortunately, that was several years ago, and I don't
remember how to do this. Can someone refresh my memory?

Bob
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Robert,

That depends what you want.
In VB6 there was added an extra array that did hold all controls in a form.
Even if it was a child of an other control.
In VBNet every control (from which the form is one) has a control collection
(array).

Which means that all controls on a form can have again controls in
acollection. Think for that just on a groupbox.

You can get all controls direct on a form just by

\\\
for each ctr as control in me.controls
ctr.text = "whatever"
'this goes because every control has a property text
next
///
The same for in a groupbox
\\\
for each ctr as control in groupbox1.controls
if TypeOf ctr is CheckBox Then
'only the checkbox and the radiobutton have a checkbox
Directcast(ctr, Checkbox.CheckState = CheckStateChecked
end if
next
///

If you want to do this for a complete form than you have to do a recursion
trick from which are more, this is the one I like (the shortest to write in
my opinion)
\\\
Private Sub SetAllCheckStateTrue(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
If TypeOf ctr Is CheckBox Then
DirectCast(ctr, CheckBox).CheckState = CheckState.Checked
End If
SetAllCheckStateTrue(ctr)
Next
End Sub
///

I hope this helps,

Cor

"Robert Boudra" <rb*****@comcast.net> schreef in bericht
news:eS**************@TK2MSFTNGP11.phx.gbl...
I remember when VB.net came out that a couple of the seminars I went to
mentioned that Control Arrays were going away and that there was a new and
better way to execute the same code when an event occurs in one of a group
of similar controls. Unfortunately, that was several years ago, and I
don't remember how to do this. Can someone refresh my memory?

Bob

Nov 21 '05 #2

P: n/a
"Robert Boudra" <rb*****@comcast.net> schrieb:
I remember when VB.net came out that a couple of the seminars I went to
mentioned that Control Arrays were going away and that there was a new and
better way to execute the same code when an event occurs in one of a group
of similar controls.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

If you want to handle events of several controls in a single event handler,
you can extend the event handler's 'Handles' list:

\\\
Private Sub Button_Click(...) Handles Button1.Click, Button2.Click,
Button3.Click
Dim SourceControl As Button = DirectCast(sender, Button)
Select Case True
Case SourceControl Is Button1
...
Case SourceControl Is Button2
...
...
End Select
End Sub
///

If your controls are created dynamically at runtime, check out the
'AddHandler' and 'RemoveHandler' statements.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Ah...that was it. Thanks,

Bob

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
"Robert Boudra" <rb*****@comcast.net> schrieb:
I remember when VB.net came out that a couple of the seminars I went to
mentioned that Control Arrays were going away and that there was a new and
better way to execute the same code when an event occurs in one of a group
of similar controls.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

If you want to handle events of several controls in a single event
handler, you can extend the event handler's 'Handles' list:

\\\
Private Sub Button_Click(...) Handles Button1.Click, Button2.Click,
Button3.Click
Dim SourceControl As Button = DirectCast(sender, Button)
Select Case True
Case SourceControl Is Button1
...
Case SourceControl Is Button2
...
...
End Select
End Sub
///

If your controls are created dynamically at runtime, check out the
'AddHandler' and 'RemoveHandler' statements.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.