How to: Call Properties and Methods of the Windows Forms Control
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at How to: Call Properties and Methods of the Windows Forms Control.
Because CWinFormsView::GetControl returns a pointer to System.Windows.Forms.Control, and not a pointer to WindowsControlLibrary1::UserControl1
, it is advisable to add a member of the user control type and initialize it in IView::OnInitialUpdate. Now you can call methods and properties using m_ViewControl
.
This topic assumes you have previously completed How to: Create the User Control and Host in a Dialog Box and How to: Create the User Control and Host MDI View.
To create the MFC host application
Open the MFC application you created in How to: Create the User Control and Host MDI View.
Add the following line to the public overrides section of the
CMFC02View
class declaration in MFC02View.h.gcroot<WindowsFormsControlLibrary1::UserControl1 ^> m_ViewControl;
Add an override for OnInitialupdate.
Display the Properties window (F4). In Class View (CTRL+SHIFT+C), select CMFC02View class. In the Properties window, select the icon for Overrides. Scoll down the list to OnInitialUpdate. Click on the drop down list and select <Add>. In MFC02View.cpp. make sure the body of the OnInitialUpdate function is as follows:
CWinFormsView::OnInitialUpdate(); m_ViewControl = safe_cast<WindowsFormsControlLibrary1::UserControl1 ^>(this->GetControl()); m_ViewControl->textBox1->Text = gcnew System::String("hi");
Build and run the project.
On the Build menu, click Build Solution.
On the Debug menu, click Start without debugging.
Notice that the text box is now initialized.