C#winform parent window inside panel container embedded subform, program main Form Design example
In the project development often encountered the parent form Embedded subform So wrote an example program, by the way probably divided the next interface module and color, the shortcomings also look at the guidance
The main form form uses the previous blog to design a flattened form
In the project development often encountered the parent form Embedded subform So wrote an example program, by the way probably divided the next interface module and color, the shortcomings also look at the guidance
The main form form uses the previous blog to design a flattened form
C # custom WinForm borderless form
Main ideas
1 this. Ismdicontainer=true; // set the parent form to be a container 2 son myson=new son (); // Instantiating a subform 3 myson.mdiparent=this; // set up a parent-child relationship for a form 4 myson.parent=pnl1; // set the container for the subform as a panel in the parent form 5 myson.show (); // Displays the subform, which is important, otherwise the subform does not display
Form design upper and lower structure, the top is the navigation bar, followed by the top form section, the Center content part of the center, the bottom is the bottom navigation panel
Can specifically write a method to do the display form
1 /// <summary>2 ///Show Form3 /// </summary>4 /// <param name= "Panel" ></param>5 /// <param name= "frm" ></param>6 Public voidShowForm (System.Windows.Forms.Panel Panel, System.Windows.Forms.Form frm)7 {8 Lock( This)9 {Ten Try One { A if( This. currentform! =NULL&& This. Currentform = =frm) - { - return; the } - Else if( This. currentform! =NULL) - { - if( This. Activemdichild! =NULL) + { - This. Activemdichild.hide (); + } A } at This. Currentform =frm; -frm. TopLevel =false; -frm. MdiParent = This; - panel. Controls.clear (); - panel. Controls.Add (frm); - frm. Show (); infrm. Dock =System.Windows.Forms.DockStyle.Fill; - This. Refresh (); to foreach(Control Iteminchfrm. Controls) + { - item. Focus (); the Break; * } $ }Panax Notoginseng Catch(System.Exception ex) - { the // + } A } the}
Child form static field
1 /// <summary>2 ///Sub-form interface Singleton element3 /// </summary>4 Public StaticForm1 Form1 =NULL;5 Public StaticForm2 Form2 =NULL;6 Public StaticFORM3 FORM3 =NULL;7 Public StaticFORM4 FORM4 =NULL;8 Public StaticFORM5 FORM5 =NULL;
Construct an initialization form each of these forms is a singleton to guarantee the uniqueness of the form
1 // instantiating a subform interface 2 Form1 = form1.getintance; 3 Form2 = form2.getintance; 4 FORM3 = form3.getintance; 5 FORM4 = form4.getintance; 6 FORM5 = form5.getintance;
Form Singleton
1 Private Staticmainform forminstance;2 Public Staticmainform getintance3 {4 Get5 {6 if(Forminstance! =NULL)7 {8 returnforminstance;9 }Ten Else One { AForminstance =Newmainform (); - returnforminstance; - } the } -}
Initialize button state
1 Private BOOLInitbutton ()2 {3 Try4 {5 This. button1. BackColor = Color.FromArgb ( -, the, the);6 This. button2. BackColor = Color.FromArgb ( -, the, the);7 This. Button3. BackColor = Color.FromArgb ( -, the, the);8 This. Button4. BackColor = Color.FromArgb ( -, the, the);9 This. button5. BackColor = Color.FromArgb ( -, the, the);Ten This. Button6. BackColor = Color.FromArgb ( -, the, the); One This. button7. BackColor = Color.FromArgb ( -, the, the); A This. Button8. BackColor = Color.FromArgb ( -, the, the); - This. Button9. BackColor = Color.FromArgb ( -, the, the); - This. Button10. BackColor = Color.FromArgb ( -, the, the); the This. button11. BackColor = Color.FromArgb ( -, the, the); - } - Catch(Exception ex) - { + return false; - } + return true; A}
Navigation button click Toggle Event
1 Private voidButton1_Click (Objectsender, EventArgs e)2 {3 Try4 {5 This. Initbutton ();6 This. button1. BackColor = Color.FromArgb ( the,129,174);7Monitor.Enter ( This. lockobj);8 if(!Formswitchflag)9 {TenFormswitchflag =true; One This. ShowForm (PNLCENTER,FORM1); AFormswitchflag =false; - } - Else the { - return; - } - } + Catch(System.Exception ex) - { + // A } at finally - { -Monitor.Exit ( This. lockobj); - } -}
Finally show the dynamic effect
Program source code project file download
Embedded subform, program main form Design example in C#winform window inside panel container