Daniel Flannery
Daniel Flannery

Reputation: 1178

C++ Checkbox's acting like radio buttons WINAPI ( No MFC )

I am trying to get 2 checkboxs in my application to act like radio buttons. IE - When one is ticked the other button will untick itself. I dont believe that this can be achieved through the properties menu so I am trying to do it in code.

I dont know much about how to do this at all so I am getting a bit lost. This is what I have so far (which Is not working)

    case BN_CLICKED:
    if(BN_CLICKED == IDC_CHECK_MW){
        SendMessage(GetDlgItem(hDlg,IDC_CHECK_MW), BM_GETCHECK, (WPARAM)0 ,(LPARAM)0) == BST_CHECKED;
    }

I may be way oof but any help would be great!

Upvotes: 1

Views: 1643

Answers (1)

Qaz
Qaz

Reputation: 61910

If you have the handles or something handy, just send a BM_SETCHECK:

int checkState = SendMessage (otherHwnd, BM_GETCHECK, 0, 0);
SendMessage (otherHwnd, BM_SETCHECK, checkState == BST_CHECKED ? BST_UNCHECKED : BST_CHECKED, 0);

This of course assumes that it can only be checked or unchecked, not in an intermediate state. I would also really reconsider your thinking, as checkboxes are meant to act as such, and radio buttons are the right tools for this behaviour.

Also, in your message switch, you want this probably:

case WM_COMMAND:
{
    if (HIWORD (wParam) == BN_CLICKED)
    {
        switch (LOWORD (wParam))
        {
            case IDC_CHECK_MW:
                //check this, uncheck that
                break;

            case IDC_OTHER_CHECK:
                //check other, uncheck first
                break;

            default:
                //something went wrong
        }
    }    
}

Upvotes: 3

Related Questions