Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#2 can't uncheck a radio-button

closed-invalid
nobody
None
5
2005-01-10
2004-11-28
Steven Weiss
No

Hi John,
here's another minor patch: in my program i need to
uncheck all radiobuttons. but with the radio_button::
check()-version of 1.6.1 you can only check() a radio-
button and the user has to uncheck the radio by himself
with low-level SendMessage()

it would be much nicer if the function would look like this:

/**
marks this button as checked. Also, any other buttons
that are part of the radio-group will be unchecked.
*/
void radio_button::check(bool bCheck = true) {
if (bCheck == false) {
send_msg(BM_SETCHECK, BST_UNCHECKED);
}
else {
send_msg(BM_SETCHECK, BST_CHECKED);

// un-click the other radio button, if any...
wnd<> prev = prev_wnd();
while ( prev) {
if ( wnd<radio_button> rb =
try_cast<radio_button>(prev) ) {
if ( rb->is_checked() ) {
rb->send_msg(BM_SETCHECK,
BST_UNCHECKED);
return;
}
}
else
break; // not radio button
prev = prev->prev_wnd();
}

wnd<> next = next_wnd();
while ( next) {
if ( wnd<radio_button> rb =
try_cast<radio_button>(next) ) {
if ( rb->is_checked() ) {
rb->send_msg(BM_SETCHECK,
BST_UNCHECKED);
return;
}
}
else
break; // not radio button
next = next->next_wnd();
}
}
}

the compatibility to written code is also given (default
parameter)

mfg steven

Discussion

  • Logged In: YES
    user_id=821057

    You should also terminate the while-loops if you find a
    window that has the WS_GROUP style set.
    Further I would prefer to put this code in a different
    function, e.g. check_group or check_adjacent.

     
  • John Torjo
    John Torjo
    2004-12-16

    Logged In: YES
    user_id=1031729

    Yes, fotzor, you're right again :)
    Patched, will be available on v1.6.2.

    To fatbull:
    First, thanks for the tip with the WS_GROUP style. You're right.

    Second, I don't think the code should go into another
    function, since the BM_SETCHECK for radio-boxes is simply
    flawed. It really makes no sense to have multiple
    radio-options, and have more than one selected at a time.

    Just for fun:
    child(IDC_RADIO1)->send_msg(BM_SETCHECK, BST_CHECKED);
    child(IDC_RADIO2)->send_msg(BM_SETCHECK, BST_CHECKED);

    This simply selects both radio-buttons, even if they belong
    to the sam group. This is what I want to avoid.

    Best,
    John

     
  • John Torjo
    John Torjo
    2004-12-16

    • status: open --> closed
     
  • Steven Weiss
    Steven Weiss
    2005-01-09

    • status: closed --> open-invalid
     
  • Steven Weiss
    Steven Weiss
    2005-01-09

    Logged In: YES
    user_id=1124235

    hi john,
    i just discovered that you made a mistake when applying the
    patch.

    try this code:
    window()->child<radio_button> (IDC_RADIO1)->check(true);
    window()->child<radio_button> (IDC_RADIO2)->check(false);

    IDC_RADIO1 is NOT checked because the check(false) will
    uncheck all radios. the correct version would be with the if
    in front of the function:

    if (bCheck == false) {
    send_msg(BM_SETCHECK, BST_UNCHECKED);
    }
    else {
    send_msg(BM_SETCHECK, BST_CHECKED);
    ...
    }

     
  • John Torjo
    John Torjo
    2005-01-10

    • status: open-invalid --> closed-invalid
     
  • John Torjo
    John Torjo
    2005-01-10

    Logged In: YES
    user_id=1031729

    Steve, I think this is not a bug.

    The whole point of having a radio button is so that
    at-most-one option is on.

    Thus:
    // this will check the RADIO1, and uncheck all others it ins
    group
    window()->child<radio_button>(IDC_RADIO1)->check(true);

    As a side-note, after doing the above, you don't need to
    manually uncheck the other button as well.

    Because this:
    // will uncheck all buttons.
    window()->child<radio_button>(IDC_RADIO2)->check(false);

    Best,
    John

     
  • Steven Weiss
    Steven Weiss
    2005-01-10

    Logged In: YES
    user_id=1124235

    i understand...i got the idea because my code was a bit
    different:

    window()->child<radio_button> (IDC_RAD_CHARZUG_1)->check(c
    == checked_type::first);
    window()->child<radio_button> (IDC_RAD_CHARZUG_2)->check(c
    == checked_type::second);

    this didn't work so i had to write it out:
    if (c == checked_type::first)
    window()->child<radio_button>
    (IDC_RAD_CHARZUG_1)->check(true);
    else if (c == checked_type::second)
    window()->child<radio_button>
    (IDC_RAD_CHARZUG_2)->check(true);
    else
    window()->child<radio_button>
    (IDC_RAD_CHARZUG_1)->check(false);

    mfg steven