Reputation: 4390
I've run into a peculiar issue while using the standard Windows Forms ComboBox
.
Essentially, if I click the ComboBox
, then click it a second time or hit escape to exit the drop-down, focus is stolen. The animation below shows how this is done:
When the focus is stolen, WM_SETFOCUS
messages are not received, and this has some interesting consequences:
TabControl
does not switch pages.As one can imagine, this leaves the application in an unacceptable state because users may not know why, or more importantly, how to resolve this focusing issue and are left thinking there is a bug because of the unresponsiveness.
After some testing, I found that focus can be restored by selecting an item from the offending ComboBox
.
I've gathered some logs from Spy++ that demonstrate the third bullet (switching focus).
As can be seen, the tab key is pressed but WM_SETFOCUS
is not monitored by Spy++.
<000434> 0025574E R WM_NCCALCSIZE fuValidRect:0000 lpncsp:007CE9CC
<000435> 0025574E S WM_NCCALCSIZE fCalcValidRects:True lpncsp:007CE9CC
<000436> 0025574E R WM_NCCALCSIZE fuValidRect:0000 lpncsp:007CE9CC
<000437> 0025574A P WM_LBUTTONUP fwKeys:0000 xPos:237 yPos:26
<000438> 0025574E P WM_LBUTTONDOWN fwKeys:MK_LBUTTON xPos:236 yPos:-7
<000439> 0025574A P WM_LBUTTONUP fwKeys:0000 xPos:237 yPos:26
<000440> 0025574A P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000441> 0025574A P WM_KEYUP nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUp:1
<000442> 0025574A P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000443> 0025574A P WM_KEYUP nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUp:1
<000444> 0025574A P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000445> 0025574A P WM_KEYUP nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUp:1
<000446> 0025574A P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
However, after we select and item in the drop-down list, focus is able to change and Spy++ captures WM_SETFOCUS
messages.
<000742> 002555E0 S WM_NCCALCSIZE fCalcValidRects:True lpncsp:007CCFA4
<000743> 002555E0 R WM_NCCALCSIZE fuValidRect:0000 lpncsp:007CCFA4
<000744> 002555E0 S WM_NCCALCSIZE fCalcValidRects:True lpncsp:007CCC94
<000745> 002555E0 R WM_NCCALCSIZE fuValidRect:0000 lpncsp:007CCC94
<000746> 0025574E R WM_KEYDOWN
<000747> 0025574A P WM_KEYUP nVirtKey:VK_DOWN cRepeat:1 ScanCode:50 fExtended:1 fAltDown:0 fRepeat:1 fUp:1
<000748> 0025574A P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000749> 0025574A S WM_KILLFOCUS hwndGetFocus:00265748
<000750> 0025574A R WM_KILLFOCUS
<000751> 00265748 S WM_SETFOCUS hwndLoseFocus:0025574A
<000752> 00265748 R WM_SETFOCUS
<000753> 00265748 P WM_KEYUP nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:1 fUp:1
<000754> 00265748 P WM_KEYDOWN nVirtKey:VK_TAB cRepeat:1 ScanCode:0F fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000755> 00265748 S WM_KILLFOCUS hwndGetFocus:00255770
<000756> 00265748 R WM_KILLFOCUS
I had initially thought that act of stealing focus was a side-effect of the ComboBox
creating a new window for the drop-down display without properly re-activating the parent window. But clicking the application icon in the task bar will cause the WM_ACTIVATE
message to appear in Spy++ without retrieving focus again.
Upvotes: 2
Views: 919
Reputation: 185
You might want to check out this topic here:
Cannot tab out of databound Winforms dropdown list
The databinding might be throwing an exception in the data validation behind the scenes.
Upvotes: 1