|
-
Mar 18th, 2003, 09:19 PM
#1
Thread Starter
Hyperactive Member
VB - Quickly determine which option button is selected
Often OptionButton controls are arranged in
control arrays. To quickly find the index of
the only selected OptionButton control you
can use the following code:
Code:
' assumes that the control array contains
three OptionButton controls
intSelected = Option(0).Value * 0 - Option(1).Value * 1 - Option(2).Value * 2
Note that the first operand is always zero,
and you can simplify the above expression as follows:
intSelected = -Option(1).Value - Option(2).Value * 2
Last edited by Ed Lampman; Mar 18th, 2003 at 09:27 PM.
-
Jul 31st, 2003, 12:06 PM
#2
Lively Member
Question for you
How can I diferentiate if the user has selected opt(0) or no buttons?
Thanks
-
Jul 31st, 2003, 05:31 PM
#3
Thread Starter
Hyperactive Member
I've always heard that the correct way to use option buttons was to load the form with one of them selected, so you don't have to worry about not selecting 'no button'. If you want the user to have the option to NOT select any, then you probably shouldn't be using option buttons, but perhaps checkboxes. Note that even if you load the form with NONE selected, once the user selects one of them, he can never NOT select one.
If the above returns 0, then button(0) was selected.
-
Aug 1st, 2003, 06:55 AM
#4
Lively Member
In my case, the user must select one and only one. If I use checkboxes I have to do more messing around to limit them to one choice. I can't pre-select an option button for them, because are salespeople are soooooooo lazy that they would accept whatever I had preselected, whether it was correct or not!
I think I'll use your code and modify the first option button to: opt(0) * 10, that way I will know that they chose the first button.
Thanks,
-
Aug 1st, 2003, 06:19 PM
#5
Thread Starter
Hyperactive Member
"Note that the first operand is always zero,
and you can simplify the above expression as follows:
intSelected = -Option(1).Value - Option(2).Value * 2"
Since -Option(0).Value * 0 is ALWAYS 0, you can just omit it.
-
Aug 22nd, 2003, 08:22 AM
#6
Why not
intSelected = -1 - Option1(0).Value - Option1(1).Value * 2 - Option1(2).Value * 3
Then:
intSelected = -1 means no option has yet been selected (or all of them have been reset to 0 by code, something I've occasionally found useful)
intSelected = 1, 2 or 3 -> option1(0), option1(1) or option1(2) has been selected.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|