Results 1 to 6 of 6

Thread: VB - Quickly determine which option button is selected

  1. #1

    Thread Starter
    Hyperactive Member Ed Lampman's Avatar
    Join Date
    Mar 2001
    Posts
    273

    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.

  2. #2
    Lively Member
    Join Date
    May 2002
    Location
    Minnesota
    Posts
    91

    Question for you

    How can I diferentiate if the user has selected opt(0) or no buttons?

    Thanks
    Stumbler

  3. #3

    Thread Starter
    Hyperactive Member Ed Lampman's Avatar
    Join Date
    Mar 2001
    Posts
    273
    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.

  4. #4
    Lively Member
    Join Date
    May 2002
    Location
    Minnesota
    Posts
    91
    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,
    Stumbler

  5. #5

    Thread Starter
    Hyperactive Member Ed Lampman's Avatar
    Join Date
    Mar 2001
    Posts
    273
    "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.

  6. #6
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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
  •  



Click Here to Expand Forum to Full Width