Microsoft KB Archive/815779

= How to dynamically add a control without hard coding the control type in Visual C++ .NET 2003 or in Visual C++ 2005 =

Article ID: 815779

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 311321.



For a Microsoft Visual C# .NET version of this article, see 815780.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::Windows::Forms
 * System::ComponentModel
 * System::Collections
 * System::Reflection

IN THIS TASK

 * SUMMARY
 * Step-by-step example
 * Code discussion
 * REFERENCES



SUMMARY
This step-by-step article describes how to dynamically add a control to a form in Microsoft Visual C++ .NET 2003 or in Microsoft Visual C++ 2005 and how to respond to the events of the control.

This article describes how to use the Assembly method of the Form type object to retrieve the assembly information. This article also describes how to create the controls dynamically by using the CreateInstance method of the System::Reflection::Assembly class, and how to change the control properties dynamically.

This article also describes how to add event handlers to the controls, and how to respond to the user actions by using these event handlers.

Back to the top

Step-by-step example
This section describes how to create a Windows Forms Application project that demonstrates how to dynamically add a control to a form in Visual C++ .NET or in Visual C++ 2005, how to dynamically add event handlers for the controls that are created, and how to respond to the events of the control.  Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++, and Windows Forms Application (.NET) is changed to Windows Forms Application. In the Name box, type CreateControls, and then click OK. By default, the Form1 form is created and opens in Design view. Add two Button controls and one ComboBox control to the Form1 form. You add controls dynamically to the top of Form1. For each control, change the Name and the Text properties as follows </li> Right-click Form1, and then click View Code.</li>  Add the following code at the top of the Form1.h file after the using directives. using namespace System::Reflection; </li>  Paste the following code in the Form1 class after the control variable declarations. // Define the Controls and Control Object private: System::Windows::Forms::TreeView *DynTreeview; private: System::Windows::Forms::TextBox *DynTextBox; private: System::Windows::Forms::ListBox *DynListBox; private: System::Windows::Forms::Control *ControlObject; </li> Switch to Design view, and then double-click Form1 to position the insertion point at the first line of the Form1_Load event.</li>  Paste the following code in the Form1_Load event. // Add event handlers to the buttons and control names to the combo btnAddControl->Click += new System::EventHandler(this,btnAddControl_Click); btnRemoveControl->Click += new System::EventHandler(this,btnRemoveControl_Click); cboControlType->Items->Add(S&quot;TreeView&quot;); cboControlType->Items->Add(S&quot;ListBox&quot;); cboControlType->Items->Add(S&quot;TextBox&quot;); </li>  Paste the following code before the Form1_Load event. private: void AddControl(String  *ControlName, String* ControlType) {   //get the assebly of the form and create a control instance Type *typ = __typeof(Form);

Assembly* asmb = typ->Assembly; ControlObject = __try_cast<System::Windows::Forms::Control*> (asmb->CreateInstance(ControlType));

//set control properties and add it to controls collection ControlObject->Name = ControlName; ControlObject->Location = System::Drawing::Point(20, 20); Controls->Add(ControlObject);

//compare control type selected, change the properties, add event handlers accordingly if (ControlType->EndsWith(S&quot;TreeView&quot;)) {       DynTreeview = __try_cast<System::Windows::Forms::TreeView *> (ControlObject); DynTreeview->Width = 200; DynTreeview->Height = 120; DynTreeview->Nodes->Add(new TreeNode(S&quot;Root&quot;)); DynTreeview->Nodes->Add(S&quot;FirstChild&quot;); DynTreeview->Nodes->Add(S&quot;SecondChild&quot;); DynTreeview->ExpandAll; DynTreeview->AfterSelect += new System::Windows::Forms::TreeViewEventHandler(this,DynTree_AfterSelect); }   else if (ControlType->EndsWith(S&quot;ListBox&quot;)) {       DynListBox = __try_cast<System::Windows::Forms::ListBox*>(ControlObject); DynListBox->Width = 200; DynListBox->Height = 120; DynListBox->Items->Add(S&quot;Apples&quot;); DynListBox->Items->Add(S&quot;Banana&quot;); DynListBox->Items->Add(S&quot;Oranges&quot;); DynListBox->SelectedIndexChanged += new System::EventHandler(this,DynCtrl_Event); }   else if (ControlType->EndsWith(S&quot;TextBox&quot;)) {       DynTextBox = __try_cast<System::Windows::Forms::TextBox  *>(ControlObject); DynTextBox->Width = 200; DynTextBox->Text = S&quot;Dynamically Added Textbox.&quot;; DynTextBox->DoubleClick += new System::EventHandler(this,DynCtrl_Event); } } private: void btnAddControl_Click(Object* sender, System::EventArgs* e) { String* CtrlType;

if (!(ControlObject == NULL)) {       btnRemoveControl->PerformClick; }

if (cboControlType->SelectedIndex < 0) {       MessageBox::Show(S&quot;Select a Control Type to add.&quot;); return; }

CtrlType = String::Concat(S&quot;System.Windows.Forms.&quot;,cboControlType->SelectedItem->ToString); this->AddControl(S&quot;myControl&quot;, CtrlType); }

private: void DynCtrl_Event(Object* sender, System::EventArgs* e) { if (sender->GetType->ToString->EndsWith(S&quot;ListBox&quot;)) {       MessageBox::Show(String::Concat(S&quot;The item you selected is: &quot;, DynListBox->SelectedItem->ToString)); }   else if (sender->GetType->ToString->EndsWith(S&quot;TextBox&quot;)) {       Clipboard::SetDataObject(DynTextBox->Text); MessageBox::Show(S&quot;The Text is copied to the clipboard.&quot;); } }

private: void btnRemoveControl_Click(Object* sender, System::EventArgs* e) { this->Controls->Remove(ControlObject); ControlObject = NULL; }

private: void DynTree_AfterSelect(Object* sender, System::Windows::Forms::TreeViewEventArgs* e) { MessageBox::Show(String::Concat(&quot;The Node you clicked on is: &quot;, e->Node->Text)); } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Press the CTRL+SHIFT+S key combination to save the project.</li> Press the CTRL+SHIFT+B key combination to build the project.</li> Press the CTRL+F5 key combination to run the application. The application runs, and Form1 appears.</li> In the combo box, click TreeView, and then click Add Control.

Note A new TreeView control is added.</li> Click any node to test the AfterClick event of the TreeView control.</li> Click Remove Control to remove the TreeView control from the Form1 window.</li> <li>Test the application for the other controls that are listed in the combo box.</li></ol>

Back to the top

Code discussion

 * In this sample, the ComboBox control contains the list of control types that you add dynamically. You populate this list in the Load event of the form.
 * You declared three specific control objects in the initial statements of the definition for Form1 so that you can use the individual functionality of these control objects in your program. By declaring specific control types, you can also access Microsoft IntelliSense while you are programming.
 * In the btnAddControl_Click event handler, determine if another control is already loaded. If another control is loaded, remove the existing control and then add the new control that was selected in the combo box. Make sure that the combo box has a valid selection. If the selection is not valid, the program generates a message box and exits the event handler without processing more statements. If the selection is valid, call the AddControl method with appropriate parameters.
 * In the AddControl method, the __typeof(Form) statement returns the type object of the Form type. You use the Assembly method of this object to retrieve an instance of the assembly where the Form class is defined. After getting the assembly instance, the asmb object is created. You use the CreateInstance method of the asmb object with a ControlType string parameter to create an instance of the control. After the control is created, you add it to the current form by using the Controls->Add method. Then, by using the EndsWith method of the String class, you check the type of control that is passed to this procedure as the second argument. Depending on the control type, you set the individual properties of the control in the if...else...if... block. You use the += operator to connect the specific event of the dynamically-added control with the event handler. For ListBox and TextBox controls, you connected the SelectedIndexChanged and the DoubleClick events with the DynCtrl_Event event handler. For the TreeView control, you used a separate event handler (DynTree_AfterSelect) to connect the control with the AfterSelect event of the TreeView control.
 * The event handler for the TreeView control requires a different signature from that of the other two controls. The second argument in the DynTree_AfterSelect event handler is of type System::Windows::Forms::TreeViewEventArgs; for the DynCtrl_Event event handler, you use type System::EventArgs. The System::Windows::Forms::TreeViewEventArgs type provides additional information, such as the properties of the selected node.

Back to the top

<div class="references_section">