Replacement for VB6 control Arrays

  • Thread starter Thread starter Robert Boudra
  • Start date Start date
R

Robert Boudra

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
 
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 said:
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.
 
Ah...that was it. Thanks,

Bob

Herfried K. Wagner said:
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.
 

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

Back
Top