A control is dynamically created through reflection. How can I use the name of the control to get the control through the emission function?

Source: Internet
Author: User

This article from: http://topic.csdn.net/t/20060712/02/4874504.html

 

The following code dynamically creates a control on the form:

String assname = typeof (system. Windows. Forms. Form). assemblyqualifiedname;

String assinfo = assemblyqualifiedname. substring (assemblyqualifiedname. indexof (","));

Type T = system. type. GetType ("system. Windows. forms." + ctrltype + assinfo, true );

Control CTRL = (Control) activator. createinstance (t );

This. Controls. Add (CTRL );

Then, use the following code to obtain the control again with a string:

Type T = This. GetType ();

Fieldinfo = T. getfield (ctrlname, bindingflags. Public | bindingflags. nonpublic | bindingflags. instance | bindingflags );

But I found that fieldinfo is null... why? Then how can we obtain the control based on the string?

 

Reply: 03:14:49

Are you sure ctrlname is a field of the current object? If it is an object in controls, you need to obtain the CONTROLS attribute, and then find the required control in the loop.

 

Reply: 09:01:46

Object o = form name. GetType (). GetField (Control name, System. Reflection. BindingFlags. Instance | System. Reflection. BindingFlags. NonPublic). GetValue (form name );
If (o! = Null)
{
CheckBox dtp = (CheckBox) o;
....

 

 

Reply: 09:25:21

Control control = this. Controls [Control name]

 

Reply: 09:47:56

To Saucer: If the control is dragged from the toolbox in Visual Studio to Form, my code can run normally. Now I am based on an XML content, after dynamically creating controls on the Form, my code cannot run successfully...

To Knight94: I have a CreateControls () function to read the XML, and then dynamically create an object on the form. After the function is executed, write "Type t = this. getType.

 

Reply: 09:53:47

To stlwj: Now I have used the Form to dynamically create a TextBox: textName, run
Object o = this. GetType (). GetField ("textName", System. Reflection. BindingFlags. Instance | System. Reflection. BindingFlags. NonPublic). GetValue (this );

Exception: System. NullReferenceException: the object reference is not set to the instance of the object.

 

Reply: 09:56:26

Is it because all objects are dynamically created, so it cannot be obtained using Type. GetField?

If so, what should I do to get the dynamically created object?

Hope you can give me some advice!

 

Reply: 10:26:18

Create a dynamic object:
First, read the name of the object to be created and its assembly path from the file.
Assembly asm = Assembly. LoadFile (Assembly file path );

Object OBJ = ASM. createinstance ("Class Name", false, bindingflags. public | bindingflags. instance | bindingflags. createinstance, null, null); if the class requires a constructor, You need to initialize the constructor.

 

Reply: 10:29:06

Private void CreateControls (XmlNodeList _ xnl, Panel _ panel)
{
Foreach (XmlNode _ xn in _ xnl)
{
String ctrlType = (XmlElement) _ xn). GetAttribute ("type ");

String assname = typeof (system. Windows. Forms. Form). assemblyqualifiedname;
String assinfo = assname. substring (assemblyqualifiedname. indexof (","));

Type T = system. type. GetType ("system. Windows. forms." + ctrltype + assemblyinformation, true );

Control CTRL = (Control) activator. createinstance (t );

_ Panel. suspendlayout ();

Ctrl. Location = new System. Drawing. Point (X, Y );
Ctrl. Name = _ xn ["Name"]. InnerText;
Ctrl. Size = new System. Drawing. Size (Width], Height );
Ctrl. Text = _ xn ["Text"]. InnerText;
Ctrl. Visible = bool. Parse (_ xn ["Visible"]. InnerText );

_ Panel. Controls. Add (ctrl );
_ Panel. ResumeLayout ();
}
}

Private void SetControlsText (XmlNodeList xnl)
{
Foreach (XmlNode _ xn in xnl)
{
Type t = this. GetType ();

FieldInfo fieldInfo = t. GetField (_ xn ["Control"]. InnerText, BindingFlags. Public | BindingFlags. NonPublic | BindingFlags. Instance | BindingFlags );
...
...
...
}
}

To Knight94: dynamically create controls and reuse types. getField () to obtain the control function. The program runs CreateControl () and SetControlText (), but fieldInfo is null at last... Why...

 

Reply: 10:33:01

The above program has a written mistake:

In CreateControl (),
String assName = typeof (System. Windows. Forms. Form). AssemblyQualifiedName;
String assInfo = assName. Substring (assemblyQualifiedName. IndexOf (","));

Type t = System. Type. GetType ("System. Windows. Forms." + ctrlType + assemblyInformation, true );

It should be
String assName = typeof (System. Windows. Forms. Form). AssemblyQualifiedName;
String assInfo = assName. Substring (assName. IndexOf (","));

Type t = System. Type. GetType ("System. Windows. Forms." + ctrlType + assInfo, true );

 

Reply: 16:30:39

To, if I dynamically create a control in the form Load event, how can I operate the control in the remaining code?

Do you know the name? If you know it, you can:
Foreach (Control ctrl in this. Controls)
{
If (CTRL. Name = yourname)
{
// Get it
}
}

 

Reply: 16:36:29

Sample code as follows:

Private control CTRL = NULL;

Change
Control CTRL = (Control) activator. createinstance (t );
With
CTRL = (Control) activator. createinstance (t );

 

Reply: 10:02:31

Type t = this. GetType (); -- Type of the form

Fieldinfo = T. getfield (ctrlname, bindingflags. Public | bindingflags. nonpublic | bindingflags. instance | bindingflags );
-- Getfield obtains the field of the Form class, and the Form class does not contain members named ctrlname.
-- The value obtained here must be null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.