Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to trigger ON_BN_CLICKED event at runtime?

For example, I want to trigger the click event of a Button in my first Dialog when I click a Button of my second Dialog.

I am using Microsoft Visual C++ 6.0 MFC.

Thanks in advance.
Posted
Updated 8-Apr-21 5:40am

I assume that you have set up for handling the button event in the second dialog.

In this event all you have to do is to send the message to the button in the first page. This can be done by:

C++
SendMessage(WM_COMMAND,
             MAKEWPARAM(IDC_MY_FIRST_BUTTON, BN_CLICKED),
            (LPARAM) m_MyFirstButton.m_hWnd);
 
Share this answer
 
Comments
agent_suwastica 10-Feb-12 5:14am    
Thanks for your reply :)
I apply your answer but it calls the click event of second button, an infinite loop is a result.
My Second Dialog is a modal dialog, is it affected or the handle of first button is not accessible?
Or for a more modern solution:

C++
CWnd* btn = &m_btn;
btn->SendMessage(
    WM_COMMAND,
    MAKEWPARAM(
        btn->GetDlgCtrlID(),
        BN_CLICKED),
    (LPARAM)btn->GetSafeHwnd()
);
 
Share this answer
 
v2
Comments
Richard Deeming 9-Apr-21 4:48am    
A "more modern solution" to a question asked nine years go about VC++ 6.0?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900