Question about universal looping through control types

A

Anthony P.

Hello Everyone,

Simple question, I'm sure, but I can't find the answer to it. So if I
want to loop through every single control on a form, I use this code:

Dim ctrl as Control
For Each ctrl on frmMain
....
Next ctrl

But I need to do something a little different and I can't quite figure
out how to do it. I have several checkboxes on the form and most,
except one, are exclusive of all others so that when one is checked
the others can't be. So I need to loop through ONLY the checkboxes on
the form and disable all of them except the current one checked (which
I know I can find through an if ctrl.text = yadda yadda statement.

The thing is I need to fire this action every time ANY checkbox is
checked and I think it's wasteful to include the same code over and
over for the checked/unchecked methods of every checkbox.

My questions are these:

1. How do I look ONLY through controls of a certain type (i.e.
checkboxes)

and

2. How do I make one method that fires for ALL checkboxes when they
are checked?

Thanks in advance!
Anthony
 
A

Anthony Papillion

Ok so here's an update...

I think I found out how to find the type of control with, surprisingly
(ok, not) the TypeOf statement.

Dim ctrl as Control
For Each ctrl on FrmMain
If TypeOf ctrl is CheckBox then
do something
End if
next ctrl

If this is correct, then I don't need help figuring out how to find
the type of control but I still need to know how to do the universal
method thing. If this is NOT correct, then I still do need help
determining the type of control lol.

Anthony
 
F

Family Tree Mike

Anthony said:
Ok so here's an update...

I think I found out how to find the type of control with, surprisingly
(ok, not) the TypeOf statement.

Dim ctrl as Control
For Each ctrl on FrmMain
If TypeOf ctrl is CheckBox then
do something
End if
next ctrl

If this is correct, then I don't need help figuring out how to find
the type of control but I still need to know how to do the universal
method thing. If this is NOT correct, then I still do need help
determining the type of control lol.

Anthony

You can create a single sub CheckChecks that takes two parameters. The
example below forces at most one checkbox (of three) to be checked. You
then attach this sub to the checkbox.CheckedChanged method in the event
properties for each of your checkboxes.

Sub CheckChecks(ByVal sender As Object, ByVal e As EventArgs) _
Handles CheckBox3.CheckedChanged, CheckBox2.CheckedChanged, _
CheckBox1.CheckedChanged
Dim cb As CheckBox = CType(sender, CheckBox)
If (cb.Checked) Then
For Each ctl As Control In Controls
If TypeOf ctl Is CheckBox Then
If (Not (ctl Is cb)) Then
CType(ctl, CheckBox).Checked = False
End If
End If
Next
End If
End Sub
 
A

Anthony Papillion

You can create a single sub CheckChecks that takes two parameters.  The
example below forces at most one checkbox (of three) to be checked.  You
then attach this sub to the checkbox.CheckedChanged method in the event
properties for each of your checkboxes.

<snip>

Thank you Mike! That is exactly what I was looking for!

Anthony
 
C

Chris

<snip>

Thank you Mike! That is exactly what I was looking for!

Anthony

Keep in mind, that check boxes are used when multiple selections are
allowed and radio buttons are used when a single, mutually exclusive
option is desired.

In general, you should not change the way controls are normally used.
Most Windows users expect, when they see check boxes, to be able to
check more than one at a time. When they see radio buttons, the know
only a single choice is allowed. You should not change this semantic
without good reason.

Chris
 

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