C # Reflection calls a method that dynamically calls a form and instantiates a form

Source: Internet
Author: User
Tags reflection

Click on that button and the form will appear.

If it's a lot of buttons, is that what we usually write?

The code is as follows Copy Code

private void Button3_Click (object sender, EventArgs e)
{
Get the button name that you clicked
String btnname = ((Button) sender). Name;
if (Btnname = = "Button1")
{
Working with Forms
}
else if (btnname = "Button2")
{
Working with Forms
}
Else
{
Working with Forms
}
}

This can only be done in a single, if more than 100 thousand will not, the code is too cumbersome.

The code is as follows Copy Code

<summary>
Open a new Subform
</summary>
<param name= "StrName" > Form's class name </param>
<param name= "AssemblyName" > The name of the class library where the form is located </param>
public static void CreateForm (String strName, String assemblyname)
{
String path = assembly option name for assemblyname;//project
String name = StrName; The name of the class
form doc = (form) assembly.load (path). CreateInstance (name);
Doc. Show ();
}
private void Button1_Click (object sender, EventArgs e)
{
Get the button name that you clicked
String btnname = ((Button) sender). Text;
CreateForm ("WindowsFormsApplication1." + Btnname, "WindowsFormsApplication1");
}

The mode of the subform you are using can also be restricted by using the following code to limit the repeated open

The code is as follows Copy Code

<summary>
Open a new Subform
</summary>
<param name= "StrName" > Form's class name </param>
<param name= "AssemblyName" > The name of the class library where the form is located </param>
<param name= "Mdiparentform" > Parent form </param>
public static void CreateForm (String strName, String assemblyname, Form Mdiparentform)
{
int Index = Strname.lastindexof (".");
String FormName = strname.substring (23);
if (! Showchildform (FormName, Mdiparentform))
{
String path = assembly option name for assemblyname;//project
String name = StrName; The name of the class
form doc = (form) assembly.load (path). CreateInstance (name);
Doc. WindowState = formwindowstate.maximized;
Doc. MdiParent = Mdiparentform;
Doc. Show ();
}
}

<summary>
Prevent subforms from opening again
</summary>
<param name= "P_childrenformtext" ></param>
<param name= "Mdiparentform" ></param>
<returns></returns>
public static bool Showchildform (string p_childrenformtext, Form mdiparentform)
{
int i;
To detect the subform of the current form in turn
for (i = 0; i < MdiParentForm.MdiChildren.Length; i++)
{
Determines whether the Text property value of the current subform is the same as the string value passed in
if (MdiParentForm.MdiChildren.Name = = P_childrenformtext)
{
If the value is the same, this subform represents the subform you want to invoke, activates the subform, and returns a value of True
MdiParentForm.MdiChildren.Activate ();
return true;
}
}
If there is no same value, it indicates that the subform being invoked is not open, and returns a value of False
return false;
}

All right, these are the personal test, we can enter the reference.

Related Article

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.