3

I am working on an MFC application (C++)

My checkbox has an event hander mapped to the ON_BN_CLICKED. It works fine when the user check/uncheck the box, i.e. the event handler is called.

However, when I check the box programmatically: ((CButton *)this->GetDlgItem(x))-> ->SetCheck(1); the event handler is not called.

What should I do in order to call the event handler programmatically?

towi_parallelism
  • 1,421
  • 1
  • 16
  • 38

2 Answers2

2

This is a normal behavior. The WM_COMMAND is sent when a "click" or "user entry" changed the button.

This is not contignous with child controls. Other child controls like an edit control also send a WM_COMMAND EN_CHANGE message when SetWindowText is executed by the program (the MFC blocks this message in a DoDataExchange).

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Thanks for your answer. The question is how I can fire up the event handler in this case? – towi_parallelism Jul 27 '15 at 12:42
  • 1
    Just call the event handler directly. - OR - Send a WM_COMMAND message with BN_CLICKED and the appropriate infos in lParam/wParam. Just look into the MSDN for the format of WM_COMMAND. – xMRi Jul 27 '15 at 12:46
2

Try to send BN_CLICKED:

this->SendMessage(WM_COMMAND, 
MAKELONG(IDC_BUTTON1, BN_CLICKED), 
((CButton *)this->GetDlgItem(x))->GetSafeHwnd());
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • Thanks, but it didn't work for me. I am sure all I needed was to tune your answer, but I'd rather to just call the handler directly as xMRi reminded me (simplicity!) – towi_parallelism Jul 27 '15 at 14:55