Android Studio Clear Radio Buttons and Wont Let Me Select Again
Why does my radio button group pick become reset each time my window regains activation?
Raymond
A customer reported (all incomplete information and cerise herrings preserved):
Nosotros have an upshot related to 2 radio buttons in a window. The code programmatically checks the 2nd button by sending the
BM_SETChequemessage. Nosotros observe that if the user clicks somewhere else on the screen (then that our application loses focus), and and then clicks on the taskbar icon to return to our application, the commencement radio button spontaneously gets selected.We watched all the messages in Spy++, and it appears that the radio push button is receiving a
WM_SETFOCUSfollowed by aWM_SETCHECK.Is this by blueprint? If not, what should I be looking for in my code that is causing this erroneous selection change to occur?
The incomplete information is that the customer didn't say how they created those radio buttons.
The red herring is that the customer said that they had a problem with their window. This suggested that they were doing a custom window implementation (because if they were using the standard dialog implementation, they would take said dialog).
But from the symptoms, it'southward clear that what'due south most likely happening is that the radio push is created every bit a BS_AUTORADIOPush button. And automatic radio buttons select themselves automatically (hence the name) when they receive focus.
That explains the message sequence of WM_SETFOCUS followed by a WM_SETBank check: The automated radio button receives focus, and in response information technology checks itself.
Therefore, the adjacent level of investigation is why the first radio push is getting focus when the window is activated.
If the application window is a custom window, then the place to await is their window's activation and focus code, to run across why focus is going to the commencement radio button instead of the second ane. Perhaps it is putting focus on the start radio push button temporarily, and then afterwards realizes, "Oh wait, I really meant to put information technology on the second radio push button." The prepare would be to get rid of the temporary focus alter and go straight to the 2nd radio button.
If the application window is a standard dialog, then we saw final time that the dialog managing director restores focus to the window that had focus last, and that you could mimic the same behavior in your ain code.
It turns out that the customer was indeed using a standard dialog, in which case the problem is that they put the dialog into an inconsistent state: They checked the second radio push button but left focus on the commencement radio push button. This is a configuration that exists nowhere in nature, and therefore when the dialog manager tries to recreate information technology (given its lack of specialized knowledge about specific controls), it can't.
The fix is to put focus on the 2d radio button also every bit setting the bank check box. In fact, you can accomplish both by setting the focus to the 2d radio push button (noting that at that place is a special process for setting focus in a dialog box) since y'all already are using automated radio buttons.
Hither's a plan that demonstrates the problem:
// scratch.rc one DIALOGEX 32, 32, 160, 38 STYLE DS_MODALFRAME | DS_SHELLFONT | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU Caption "Test" FONT 9, "MS Shell Dlg" BEGIN CONTROL "Beginning", 100, "Button", WS_GROUP | WS_TABSTOP | BS_AUTORADIOBUTTON, 4, 4, 152, 13 Command "2nd", 101, "Push",BS_AUTORADIOBUTTON, 4, 20, 152, 13 END // scratch.cpp #include <windows.h> #include <windowsx.h> INT_PTR CALLBACK DlgProc( HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_INITDIALOG: SetFocus(GetDlgItem(hdlg, 100)); CheckRadioButton(hdlg, 100, 101, 101); return FALSE; instance WM_COMMAND: switch (GET_WM_COMMAND_ID(wParam, lParam)) { instance 100: instance 101: CheckRadioButton(hdlg, 100, 101, GET_WM_COMMAND_ID(wParam, lParam)); break; case IDCANCEL: EndDialog(hdlg, 0); break; } } return False; } int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nShowCmd) { DialogBox(hinst, MAKEINTRESOURCE(1), nullptr, DlgProc); return 0; } Observe that we set focus to the first button merely check the 2nd button. When the dialog regains focus, the second button volition burn down a WM_COMMAND considering information technology thinks information technology was clicked on, and in response the dialog box moves the option to the second button.
The fix here is actually pretty uncomplicated: Let the dialog manager handle the initial focus. Merely delete the Gear upFocus telephone call and render Truthful, which means, "Hey, dialog manager, you lot exercise the focus matter, don't worry about me."
Some other fix is to remove the code that updates the radio buttons in response to the WM_COMMAND bulletin. (I.east., become rid of the unabridged case 100 and case 101 handlers.) Again, simply allow the dialog manager practise the usual matter, and everything will work out only fine.
It's slap-up when you can fix a bug by deleting code.
wagnerbroubtrall73.blogspot.com
Source: https://devblogs.microsoft.com/oldnewthing/20140522-00/?p=933
0 Response to "Android Studio Clear Radio Buttons and Wont Let Me Select Again"
Post a Comment