سؤال

. I am new to c++ and trying to get the value of the radio button for the use in my application. But i am not able to get correct state of my radio button . Here is what i have coded :

hDecRB = CreateWindow(TEXT("BUTTON"), TEXT("Decimal Result"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,20, 80, 150, 30, hWnd, NULL, NULL, NULL);
                        hDecRB = CreateWindow(TEXT("BUTTON"), TEXT("Binary Result"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,20, 120, 150, 30, hWnd, NULL, NULL, NULL);
                        hDecRB = CreateWindow(TEXT("BUTTON"), TEXT("Octal Result"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,20, 160, 150, 30, hWnd, NULL, NULL, NULL);
                        hDecRB = CreateWindow(TEXT("BUTTON"), TEXT("Hex Result"), WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,20, 200, 150, 30, hWnd, NULL, NULL, NULL);


                       if(BM_GETCHECK==BST_CHECKED)
                       {   
                           flag=Button_GetState(hBinRB);

                           MessageBox(NULL,"CHECKED","sachin",MB_OK);
                       }
                       else
                       {
                           MessageBox(NULL,"Binary not checked","sachin",MB_OK);
                            MessageBox(NULL,(LPCSTR)flag,"sachin",MB_OK);
                       }

but when i click on my dedecated button to check the radio button state it shows me unchecked in both checked and unchecked conditions.. Please help me to get out of this . . .

هل كانت مفيدة؟

المحلول

if(BM_GETCHECK==BST_CHECKED)

This condition will always evaluate to false. That is why it is telling you the button is unchecked regardless of the button state.

In your case, you probably want something like this:

if(Button_GetState(hDecRB) == BST_CHECKED) {
    MessageBox(NULL,"CHECKED","sachin",MB_OK);
}
else {
    MessageBox(NULL,"Binary not checked","sachin",MB_OK);
    MessageBox(NULL,(LPCSTR)flag,"sachin",MB_OK);
}

Also you need to store each button in a different HWND. You are using hDecRB for each one, which is being overwritten each time you reassign it.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb775986(v=vs.85).aspx

نصائح أخرى

If your button was created as part of a dialog template (dialog resource), then you would use

if (IsDlgButtonChecked(hDlg, buttonID) == BST_CHECKED)

Using dialog template is much easier than creating button windows manually like your code does...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top