Click here to Skip to main content
15,940,122 members
Articles / Desktop Programming / MFC

MFC KillFocus Derived CEdit

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
11 Jun 2024CPOL3 min read 4K   64   2   3
MFC KillFocus Derived CEdit to solve the Q&A question which has been reposted

Table of Content

The example code is hosted on Github.


This tip is written in response to Member 14594285's Q&A question on Kill focus derived class cedit which he reposted more than once. Hopefully this tip can help him to resolve his issue.


First we create a test application by adding two static controls, two edit controls and a button to switch focus away from the first edit control to second edit control.

Image 1

Secondly, we right-click on the first edit control to add variable to control it.

Image 2

The "Add Control Variable" dialog appears. Fill in the control name and make sure the "Category" is Control and "Variable Type" is CEdit. Remember to add variable for the second edit control as well.

Image 3

Next, we are going to create our CEdit derived class. Go to "Solution Explorer" and right-click on your MFC project and select "Class Wizard..."

Image 4

The "Class Wizard" appears. Click on the right dropdown of "Add Class..."

Image 5

The "MFC Class..." dropdown appears and click on it.

Image 6

The "Add MFC Class" dialog appears. Give your derived class a proper name under "Class name". I call mine CDerivedEdit. Remember to change from CWnd to CEdit for the "Base class" dropdown on the right. Click Ok when done.

Image 7

We are back to Class Wizard dialog. Now we are going to add the function handler for WM_KILLFOCUS.

Image 8

If for some reason, you cannot add the function handler for WM_KILLFOCUS. You can add it manually in code. Open the CDerivedEdit.h in your editor and add this function declaration to the CDerivedEdit class. Don't worry about afx_msg; it is to tell Visual Studio that this function is to handle message. afx_msg is defined to be nothing. AFX is the old name of MFC which is never released.

afx_msg void OnKillFocus(CWnd* pNewWnd);

Open CDerivedEdit.cpp in your editor and go to the BEGIN_MESSAGE_MAP section.




Add the OnKillFocus as below if it haven't been added by Class Wizard. The only new line is the OutputDebugStringA call to display in the debugger that OnKillFocus has been called.

void CDerivedEdit::OnKillFocus(CWnd* pNewWnd)

    OutputDebugStringA("OnKillFocus called.\n");

For the button function action, double-click on the button, a function will be added automatically. Add m_edtAddress.SetFocus(); to the function. This kills focus of the m_edtName by setting focus to m_edtAddress.

void CTestDerivedEditDlg::OnBnClickedBtnFocus()

The last thing we need to do is to change the class of m_edtName from CEdit to CDerivedEdit. First, open your dialog header in your editor and include the CDerivedEdit.h

#include "CDerivedEdit.h"

Change the class of m_edtName from CEdit

CEdit m_edtName;

to CDerivedEdit

CDerivedEdit m_edtName;

We are ready to test our CDerivedEdit! Debug the application in Visual Studio. Click on the first edit control to set focus on it and then click the button to switch focus to second edit control. You can also kill focus by selecting the second edit control with your mouse click.

Image 9

You should see this line displayed in your debugger output.

OnKillFocus called.

Hurray! Our code works.


  • 12th June 2024: First Release and fixed the bug of CDerivedEdit inherited from CWnd instead of CEdit.


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

Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

Questionfor the long time i was looking for this Pin
Praveen Kumar Katiyar14-Jun-24 3:28
professionalPraveen Kumar Katiyar14-Jun-24 3:28 
AnswerRe: for the long time i was looking for this Pin
Shao Voon Wong14-Jun-24 15:59
mvaShao Voon Wong14-Jun-24 15:59 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA12-Jun-24 2:51
professionalȘtefan-Mihai MOGA12-Jun-24 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.