Names of individual controls in option group

P

Petr Danes

I have a set of option buttons in a group. They work fine, I use the name of
the option group to determine which one was selected. But the buttons are
also clustered into subgroups and I take different actions, depending on
which subgroup contains the selected option.

Currently, my code does it with either a SELECT CASE statement or a compound
IF-THEN-ELSEIF statement (I'm still building it). This means that whenever I
change something about the option button subgrouping, I must also change my
code. I would like to be able to look at the label attached to each button
in the group, since the format of the label tells me to which subgroup it
belongs. I can then code for this and arrange the option buttons as needed
without changing the code.

Intellisense does not offer anything helpful and I have found nothing in the
archives dealing with this. Is there a way to get from option group name to
label of selected option button?

Pete
 
A

Allen Browne

An option group has a Controls collection, containing the option buttons in
the group.

Each option button has a controls collection, and the attached label is the
only member of that collection.

This example gets the Caption of the label attached to optSomeButton:
Debug.Print Me.optSomeButton.Controls(0).Caption
 
P

Petr Danes

Hello Allen,

thank you, as usual, your answers makes me feel like I'm making mudpies
while the grownups are working. ;)

Interesting that both the option buttons and their labels are members of the
group. So I can get to a label control in either of two ways:

grpsymbol.Controls(13).controls(0).caption
grpsymbol.Controls(14).caption

both give me the label for the seventh option button.

But another point comes up: is there a syntax that will -directly- give me
the selected control in the group? Currently, I have them in order, with
each having its own distinct value. But if I have, say, options 1-6, and
suddenly decide I no longer need number 3, I can delete it, but options
buttons 4-6 will no longer have the ordinal positions 4-6, but rather 3-5 in
the group. I would need to renumber everything after the deleted control to
use those directly, and it would not be as modular as I would like.

Of course, I can poll the controls in the group, using code like this:

For i = 1 To grpSymbol.Controls.Count - 1 Step 2
Debug.Print i, grpSymbol.Controls(i).Name, grpSymbol.Controls(i).Name,
grpSymbol.Controls(i).OptionValue;
If grpSymbol.Value = grpSymbol.Controls(i).OptionValue Then Debug.Print
" <--- This one";
Debug.Print
Next i

but it would be nice if I could get the selected option control directly,
something like:

grpsymbol.SelectedItem

or some such. Is there anything like that?

Pete
 
A

Allen Browne

Right: you can compare the Value of the group to the OptionValue of the each
option button until you find the right button.

Or, if the button's start from OptionValue 1 and go sequentially, it might
be:
Me.grp.Controls(Me.grp.Value - 1) ...
 
P

Petr Danes

Okay, thanks. I was hoping for a more direct method, but I guess you can't
have everything, and these work well enough.

Pete
 

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