Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used a derived class for CEdit but I must use OnKillFocus in derived class of CEdit..how must I write?

What I have tried:

I searched examples on internet but I don't find
Posted
Comments
0x01AA 11-Jun-24 13:26pm    
And you really assume that we know this inherited class from CEdit and that we can help you?
Shao Voon Wong 12-Jun-24 4:16am    
I have posted a tip to your question: https://www.codeproject.com/Tips/5383694/MFC-KillFocus-Derived-CEdit

Hope the solution works for you!
Shao Voon Wong 12-Jun-24 6:12am    
I have fixed a bug with my tip where CDerivedEdit is inheriting from CWnd instead of CEdit. Please download the code again.

To write a method that causes the derived input field (CEdit) to lose focus, you can perhaps use the SetFocus function to set the focus to another control or the main window. Specifically, you should use OnKillFocus method to set the focus to another window, e.g. the parent window of the input field or another control.

C++
class CMyEdit : public CEdit
{
protected:
    afx_msg void OnKillFocus(CWnd* pNewWnd);
    DECLARE_MESSAGE_MAP()
};

C++
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
    ON_WM_KILLFOCUS()
END_MESSAGE_MAP()

void CMyEdit::OnKillFocus(CWnd* pNewWnd)
{
    // Call the base class implementation
    __super::OnKillFocus(pNewWnd);

    // Set focus to the parent window
    CWnd* pParent = GetParent();
    if (pParent != nullptr) {
        pParent->SetFocus();
    }
}
 
Share this answer
 
v2
Not a solution, but an answer.
And you really assume that we know this inherited class from CEdit and that we can help you?
 
Share this answer
 
v2
If you want to know how to use a control then go first to the documentation: CEdit Class | Microsoft Learn[^].
 
Share this answer
 
I found some code in an article here[^] that shows how to do this. Here are the steps :

First - declare the method in the control's class declaration :
C++
afx_msg void OnKillFocus( CWnd* pNewWnd );
Second - add an entry for the message in the control's message map :
C++
ON_WM_KILLFOCUS()
Third - add the implementation of the override :
C++
void CColourPopup::OnKillFocus( CWnd* pNewWnd )
{
	__super::OnKillFocus( pNewWnd );
    ReleaseCapture();
}
This is the implementation for this particular control and may not necessary in your implementation. Especially if you do not explicitly capture the mouse.
 
Share this answer
 

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