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.