Click here to Skip to main content
12,245,984 members (42,907 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked

Using WinForms controls in an MFC dialog

, 20 Dec 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
This article is a simple introduction to using the CWinFormsControl MFC class to put a Windows Forms control on an MFC dialog.


A few days ago, someone asked on the Code Project VC++ forum whether he can use the Dundas WinForms controls on his MFC dialogs. The answer is - yes, that's entirely possible, and pretty easy to do with VC++ 2005.

This article is a simple introduction to using the CWinFormsControl MFC class to put a Windows Forms control on an MFC dialog. The article demonstrates this using a small dialog based application that has a .NET MaskedTextBox control placed on it. The MaskInputRejected event is handled through a function declared in the MFC dialog class.

Steps to put the .NET control in the MFC dialog

  1. Create a fresh MFC dialog based application using VC++ 2005. And turn on /clr compilation.

  2. Add the following header file include to your stdafx.h

    #include <afxwinforms.h>
  3. Modify your dialog resource so it has the controls shown in the screenshot above, with one change. Where you see the MaskedTextBox control in the screenshot, put a static control. You should end up with the following controls.

    IDC_MASKED_EDIT1 We'll map this to the MaskedTextBox control
    IDC_EDITMASK CEdit m_Mask
    IDC_EDIT_STATUS CEdit m_StatusEdit (DDX)
    IDC_STATIC "Set Mask"
    IDC_STATIC "Enter Text"
    Table 1 : The controls that you need to add to the dialog resource
  4. Add the CWinFormsControl variable to your dialog header file :

    CWinFormsControl<System::Windows::Forms::MaskedTextBox> m_MaskedEdit;

    The CWinFormsControl class provides the functionality to host a .NET control in an MFC application.

  5. In your dialog class's DoDataExchange, add a call to DDX_ManagedControl :

    void CDialogFormsDlg::DoDataExchange(CDataExchange* pDX)
        DDX_ManagedControl(pDX, IDC_MASKED_EDIT1, m_MaskedEdit);
    . . .

    This creates the .NET control and associates it with that resource ID.

  6. Add the event handler declaration your header file.

    void OnMaskInputRejected(System::Object^, 
  7. Setup the delegate map in a public section of your dialog class declaration.

    BEGIN_DELEGATE_MAP( CDialogFormsDlg )
        EVENT_DELEGATE_ENTRY( OnMaskInputRejected, System::Object^, 
            System::Windows::Forms::MaskInputRejectedEventArgs^ )

    The delegate map allows us to use an MFC class function as a delegate by calling MAKE_DELEGATE on it.

  8. Setup the OnBnSetMask function.

    void CDialogFormsDlg::OnBnSetMask()
       CString strMask; 
       m_MaskedEdit->Mask = gcnew System::String(strMask);
  9. Setup the MaskedTextBox control in OnInitDialog.

    m_MaskedEdit->PromptChar = L' ';
    m_MaskedEdit->MaskInputRejected +=
        MAKE_DELEGATE( System::Windows::Forms::MaskInputRejectedEventHandler, 
  10. Add the OnMaskInputRejected function to the dialog class.

    void CDialogFormsDlg::OnMaskInputRejected(System::Object^, 
        System::Windows::Forms::MaskInputRejectedEventArgs^ args)
        m_StatusEdit.SetWindowText(L"You've hit the max length of the mask.");
      else if(args->Position == m_MaskedEdit->Mask->Length)
        m_StatusEdit.SetWindowText(L"You are at the end of the mask.");
        m_StatusEdit.SetWindowText(L"Bad entry. Check your input!");
  11. That's it. We are all done. Build and run the application. What you see is an MFC dialog that contains a WinForms control.


This should help you reuse your existing MFC applications and at the same time use new hyper looking .NET UI controls that are released in the market. Please use the article forum to provide feedback about the article or to post any questions you may have.


  • Dec 20, 2005 - First published.


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


About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 16 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (14 consecutive awards as of 2015).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

QuestionWhen done in MFC ActiveX Controls, Remains Inactive!? Pin
.Suchit16-Jul-07 0:09
member.Suchit16-Jul-07 0:09 
GeneralToolStrip & Menustrip Pin
Coolsunil19-Feb-07 20:40
memberCoolsunil19-Feb-07 20:40 
QuestionEver tried using WinForms controls inside a MFC ActiveX ? Pin
mykel3-Jan-07 23:44
membermykel3-Jan-07 23:44 
GeneralCombining MFC and Winforms Pin
beausmom5-Dec-06 16:26
memberbeausmom5-Dec-06 16:26 
GeneralRe: Combining MFC and Winforms Pin
Aamir Butt1-Mar-07 20:34
memberAamir Butt1-Mar-07 20:34 
QuestionUsing CWinFormsControl in MFC Projects Pin
Sabarish_s22-Nov-06 3:39
memberSabarish_s22-Nov-06 3:39 
AnswerRe: Using CWinFormsControl in MFC Projects Pin
Nishant Sivakumar22-Nov-06 5:49
editorNishant Sivakumar22-Nov-06 5:49 
Generalsetting /clr for source file only Pin
Dave Calkins8-Sep-06 3:49
memberDave Calkins8-Sep-06 3:49 
GeneralRe: setting /clr for source file only Pin
Redjo17-Oct-06 0:25
memberRedjo17-Oct-06 0:25 
QuestionRe: setting /clr for source file only Pin
jkhax0r30-Jan-07 6:51
memberjkhax0r30-Jan-07 6:51 
Generalresult when .NET 2.0 runtime not installed Pin
Dave Calkins7-Sep-06 4:00
memberDave Calkins7-Sep-06 4:00 
General.Net Framework Pin
bcharra20-Jul-06 9:59
memberbcharra20-Jul-06 9:59 
GeneralLockup problem using CWinFormsControl Pin
JeffBoenig12-Jul-06 11:13
memberJeffBoenig12-Jul-06 11:13 
GeneralRe: Lockup problem using CWinFormsControl Pin
Nishant Sivakumar12-Jul-06 11:23
staffNishant Sivakumar12-Jul-06 11:23 
GeneralRe: Lockup problem using CWinFormsControl Pin
JeffBoenig12-Jul-06 11:40
memberJeffBoenig12-Jul-06 11:40 
GeneralRe: Lockup problem using CWinFormsControl Pin
Jerry Jeremiah10-Sep-08 16:53
memberJerry Jeremiah10-Sep-08 16:53 
Generalproblems with embedding .NET control in header file when calling MFC application is not compiled with /clr Pin
raghu venkataraman9-May-06 8:26
memberraghu venkataraman9-May-06 8:26 
GeneralRe: problems with embedding .NET control in header file when calling MFC application is not compiled with /clr Pin
Nishant Sivakumar12-Jul-06 11:21
staffNishant Sivakumar12-Jul-06 11:21 
QuestionDatagrid in MFC Dialog Pin
Vanathi.P2-Apr-06 21:47
memberVanathi.P2-Apr-06 21:47 
AnswerRe: Datagrid in MFC Dialog Pin
Kulch4212-Jun-06 12:02
memberKulch4212-Jun-06 12:02 
GeneralRe: Datagrid in MFC Dialog Pin
ddillon200513-May-07 20:40
memberddillon200513-May-07 20:40 
AnswerRe: Datagrid in MFC Dialog Pin
Snakefoot4-Sep-08 2:17
memberSnakefoot4-Sep-08 2:17 
GeneralSemi Blank Control Pin
tahngarth31-Mar-06 13:05
membertahngarth31-Mar-06 13:05 
QuestionMFC project to Form base project Pin
msonawane9-Jan-06 14:35
membermsonawane9-Jan-06 14:35 
AnswerRe: MFC project to Form base project Pin
Nishant Sivakumar10-Jan-06 4:30
staffNishant Sivakumar10-Jan-06 4:30 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 20 Dec 2005
Article Copyright 2005 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid