How to dynamically create controls based on strings on. NET

Source: Internet
Author: User

Using strings to dynamically create controls in. NET is achieved through reflection.

First, the system. type. GetType method is used to obtain the type instance of the control specified in the string.

Pay attention to the syntax of this string. According to the msdn explanation:

  1. Hiding by name and signature takes into account all the parts of the signature, including custom modifiers, return types, parameter types, tags, and unmanaged call conventions. This is a binary comparison.
  2. For reflection, attributes and events are hidden by name and signature. If the base class has properties that contain both get accessors and set accessors, but only get accessors In the derived class, the derived class property hides the base class property, you cannot access the setup program of the base class.
  3. Custom features are not part of a general type system.

No search is performed for arrays or COM types unless they have been loaded into available class tables.

TypenameIt can be a simple type name, a type name containing the namespace, or a complex name containing the Assembly name specification.

IfTypenameOnly contains the type name. This method is first searched in the program of the called object, and then in the mscorlib. dll program. If typename is fully qualified by partial or complete assembly names, this method is searched in the specified assembly.

Assemblyqualifiedname can return a fully qualified type name (including the nested type and Assembly name ). All compilers that support the Common Language Runtime Library will issue simple names of Nested classes, and when queried, reflection constructs a mangled name according to the following conventions.

For example, a fully qualified name of a class may be in the following format:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly

However, directly use type. GetType ("system. Windows. Forms. textbox") to obtain that type is null. This is because windows. forms assembly is a public assembly and is located in the Assembly Cache. This Assembly has different versions. To determine the version used, we must not only provide the Assembly name, the Assembly version and strong name are also provided. According to this idea, it is in use. net Framework 1.1, write this sentence as type. getType ("system. windows. forms. checkbox, system. windows. forms, version = 1.0.5000.0, culture = neutral, publickeytoken = b77a5c561934e089 "). Now there is no problem. The question is, how do we obtain the version and strong name of the windows. Forms Assembly used? You can use GetType (checkbox ). assemblyqualifiedname syntax, once this information is obtained, we can use this information for any other control, because they all come from the same version of Windows. forms assembly.

Using the method mentioned above, you can use system. activator. createinstance to create a Textbox Control:

Public static void createcontrol (string controltype, Form, int positionx, int positiony)
{
Try
{
String assemblyqualifiedname = typeof (system. Windows. Forms. Form). assemblyqualifiedname;
String assemblyinformation = assemblyqualifiedname. substring (assemblyqualifiedname. indexof (","));

Type ty = type. GetType (controltype + assemblyinformation );
Control newcontrol = (Control) system. activator. createinstance (TY );
Form. suspendlayout ();
Newcontrol. Location = new system. Drawing. Point (positionx, positiony );
Newcontrol. Name = Ty. Name + form. Controls. Count. tostring ();
Form. Controls. Add (newcontrol );
Form. resumelayout ();
}
Catch (exception ex)
{
Throw ex;
}
}

Call: createcontrol ("system. Windows. Forms. textbox", this, 10, 10 );

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.