How to: In the case of hard coding of the control type, the activation add control (from MSDN) in Visual C # .NET

xiaoxiao2021-03-06  72

For Microsoft Visual Basic .NET versions of this article, see

311321. This article refers to the following Microsoft .NET Framework Class library namespace:

System.Reflection

This task content

Summary

Step-by-step sample code discussion reference

SUMMARY This article describes how to dynamically add controls and how to respond to controls in Visual C # .NET.

Back to top

Step-by-step examples This section describes how to create a project to demonstrate how to dynamically add controls to the form in Visual C # .NET and how to respond to controls.

Start Microsoft Visual Studio .NET. On the File menu, point to "New" and click Project. Click "Visual C # item" under Project Type, and then click "Windows Application" under Modular. By default, Form1 will be added to the project. Add the following code to the top of the "Form1 Code" window: Using System.Reflection; drag two "Button" controls from the toolbox and a "ComboBox" control, then place these controls near the bottom of Form1. You will dynamically add these controls to the top of the form. In the Properties window, change the "Name" and "Text" properties of these controls as follows:

Control Name Text Property Button1BtnaddControl Adding Control Button2BtnremoveControl Delete Controls ComboBox1cboControlType Select Control Type Paste the following code as the first few statements defined by Form1: TreeView DyntreeView;

Textbox DyntextBox;

Listbox Dynlistbox;

Control ControlObject; Switch to the Design view, then double-click "Form1" to position the insertion point to the first line of the "form1_load" event. Add the following code to "Form1_Load" event: this.btnaddControl.click = new system.eventhandler (this.btnaddControl_click);

This.btnremoveControl.Click = new system.EventHandler (this.btnremovecontrol_click);

CboControlType.Items.addrange (New Object [3] {"TreeView", "ListBox", "TextBox"}); Paste the following code to Form1's "Windows Form Designer Generated Code" area: Private Void AddControl String ControlName, String ControlType)

{

System.Reflection.Assembly ASM;

ASM = typeof (form) .assembly

ControlObject = (system.windows.forms.control) asm.createInstance (ControlType);

ControlObject.name = controlname;

ControlObject.location = new system.drawing.point (20, 20);

This.Controls.add (controlObject);

IF (ControlType.endSwith ("TreeView")))

{

DyntreeView = (System.Windows.Forms.treeView) ControlObject;

DyntreeView.width = 200;

DyntreeView.height = 120; DyntreeView.Nodes.Add (New Treenode ("root");

DyntreeView.Nodes.Add ("firstchild");

DyntreeView.Nodes.Add ("Secondchild");

DyntreeView.expandall ();

DyntreeView.AfTerselect = new system.windows.Forms.treeViewEventHandler (DYNTREE_AFTERSELECT);

}

ELSE IF (ControlType.endswith ("ListBox"))

{

Dynlistbox = (system.windows.forms.listbox) ControlObject;

Dynlistbox.width = 200;

Dynlistbox.height = 120;

Dynlistbox.Items.addrange (New Object [3] {"Apples", "Banana", "Oranges"});

Dynlistbox.selected = new system.eventhandler (Dynctrl_Event);

}

Else IF (ControlType.endSwith ("TextBox")))

{

DyntextBox = (System.Windows.Forms.TextBox) ControlObject;

Dyntextbox.width = 200;

DyntextBox.text = "Dynamically Added TextBox."

DyntextBox.doubleClick = new system.eventhandler (Dynctrl_Event);

}

}

Private void btnaddControl_click (Object Sender, System.Eventargs E)

{

String Ctrltype;

if (! (! (! (!))

{

BtnremoveControl.PerformClick ();

}

IF (CBOControlType.Selected "0)

{

MessageBox.show ("SELECT A Control Type to Add.");

Return;

}

CtrlType = "System.Windows.Forms." CboControlType.SelectedItem.toString ();

This.AddControl ("MyControl", CtrlType;

}

Private void Dynctrl_Event (Object Sender, System.EventArgs E)

{

IF (Sender.gettype (). TOSTRING (). Endswith ("ListBox"))

{

Messagebox.show ("The item you selected is:" DynlistBox.SelectedItem.toString ());

}

Else if (Sender.gettype (). TOSTRING (). Endswith ("TextBox"))

{

Clipboard.SetDataObject (DyntextBox.Text);

Messagebox.show ("The text is copy to the clipboard.");

}

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 ("The Node You Clicked On IS:" E.Node.Text);

} Save the project. To run the project, click Start in the Debug menu. In the combo box, click "TreeView" as the control type, then click Add Control. You will notice that a new TreeView control has been added. Click any node to test the "AfterClick" event of TreeView.

Back to top

Code discussion

In this example, the ComboBox control contains a list of control types to be dynamically added. You need to pop up this list into the form of the form. Because you declare three specific control objects in the previous few statements defined by Form1, you can use these control objects in the program. By declaring a specific control type, you can also access Microsoft IntelliSense when programming. In the BTnadDControl_Click event handler, check to determine if another control has been loaded. If so, delete an existing control, then add the new control selected in the combo box. It also needs to be ensured that the selection in the combo box is valid. If the selection is invalid, the program will generate a message box and exit the event handler without processes the statement. If you choose to be valid, you can call the AddControl method using the appropriate parameter. In the AddControl method, you have declared ASM to System.Reflection.Assembly types. The TypeOf statement returns a type object of the form type. Then use this object's assembly method to retrieve an instance of the assembly that defines the FORM class. After the assembly, an ASM object will be created. You created an instance of the control using the CreateInstance method of the ASM object and a ControlType string parameter. After creating this control, you add it to the current form using this.controls.add method. Then, by using the endswith method of the String class, you check the type of control to pass the control as the second parameter to this process. According to the type of control, you set the individual properties of the control in the if ... Else ... if ... code block. You use the = operator to connect a specific event of the dynamically added control to the event handler. For the ListBox and TextBox controls, you connect the SELECTEDIDEXCHANGED and DoubleClick events to the Dynctrl_Event event handler. For TreeView controls, you have used a separate event handler (Dyntree_AFTersect) connects this control to the AFTERSELECT event of the TreeView control. The event handler of the TreeView control requires a different signature with other two controls. The type of the second parameter of the "Dyntree_AfTerselect" event handler is "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 top

For more information on adding controls on the form when the control type is hardcod, please visit the following Microsoft Developer Network (MSDN) Web site:

Add Controls to Windows FormShtp: //msdn.microsoft.com/library/en-us/vbcon/html/vbtskaddingControlstoform.asp For more information on Event Handle, visit the following MSDN Web site:

Events and delegateshtp: //msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsdelegates.asp

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002)

转载请注明原文地址:https://www.9cbs.com/read-109019.html

New Post(0)