2

I have a dialog box with several comboboxes in it as a member variables. The control wizard lets me create handlers for the comboboxes for the CBN_KILLFOCUS message. For example, one such handler is automatically called

void MyDlg::OnKillfocusMyCombo()

My expectation is that this handler would be called just as soon as I tab out of it. But it doesn't get called.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
rtischer8277
  • 496
  • 6
  • 27
  • It works like charm here. Give us some more information. – Jabberwocky Apr 14 '14 at 20:11
  • 1
    Use Spy++ and you will see that the message arrives. – xMRi Apr 15 '14 at 06:03
  • Yes, using Spy+ I see the WM_KILLFOCUS. Looking at the message's properties I see that the CB's parent is the MyDlg window. This is what I expect too. MSDN says the WM_KILLFOCUS message gets sent to the parent which would be the MyDlg. So I am still puzzled as to why my handler isn't catching the S message. I can do a message Find in Spy+, type in the hwnd and it finds the CB object, but I don't see it highlighted in the Spy+ Window object list and I don't see the hwnd I got from the hit when I drag the Finder Tool target over the CB edit box. Why isn't there an hwnd match somewhere? – rtischer8277 Apr 15 '14 at 13:29
  • To simplify things I made a simple MFC app with a ribbon. I put a combobox on the ribbon. I made a handler for the WM_CREATE message so the CB's edit box would work and I made a handler for the WM_KILLFOCUS message. Leaving the CB's edit box does not send a WM_KILLFOCUS message to its handler. In fact, the handler code seems to have been optimized away (the debug ball is empty). What am I missing? Spy+ says the message is being generated when I leave the CB edit box. – rtischer8277 Apr 16 '14 at 12:26
  • When I get time, I'll submit the bug and workaround as a bug report. – rtischer8277 Dec 21 '18 at 12:38
  • Double-check the message map entry from the `BEGIN_MESSAGE_MAP()..` section. Verify that it's `ON_CBN_KILLFOCUS(IDC_THE_COMBO, &CMyDlg::OnKillfocusMyCombo)`. I've messed that one up before, other macros e.g. `ON_EN_KILLFOCUS` compile fine but don't work. – Jonathan Lidbeck Sep 22 '20 at 19:08

1 Answers1

1

I ran into the same issue. This is a bug in MFC. (It's over 4 years later and it is still there.) Somehow ON_CBN_KILLFOCUS handler is never called although the Win32 CBN_KILLFOCUS notification itself is broadcast.

To fix this override the WindowProc for the dialog manually (Win32-way):

LRESULT CMyDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    // TODO: Add your specialized code here and/or call the base class

    //Fix for the bug in MFC
    if(message == WM_COMMAND)
    {
        if(HIWORD(wParam) == CBN_KILLFOCUS &&
            LOWORD(wParam) == Your_ComboBox_ID)
        {
            OnCbnKillfocusComboBox();
        }
    }

    return CDialog::WindowProc(message, wParam, lParam);
}
c00000fd
  • 20,994
  • 29
  • 177
  • 400