Many friends, when learning C #, the production of WinForm small program always has a problem, if we set in the parent form is clicked a button, open a subform, continuous click always appear continuously the same form, but we sometimes just want to open a form, how to do?
Hehe, I come to tell you, I hope to help you, of course, I understand the mood of beginners, can read, to ensure that the code is available, I hope to help you.
//set the public method to open only once Public Static intCXYPX (Form Frmmdifather,stringstrmdichild) { intBreturn =-1; for(inti =0; i < frmMdiFather.MdiChildren.Length; i++) { if(Frmmdifather.mdichildren[i]. Name = =strmdichild) {Frmmdifather.mdichildren[i]. BringToFront (); Breturn=i; Break; } } returnBreturn; }
//then add the following code to your button click event:Private voidAdd Billing Toolstripmenuitem_click (Objectsender, EventArgs e) { intindex = CXYPX ( This,"ZD");//Remember, the ZD here is the name of the subform. if(Index = =-1) {ZD tjzb=NewZD ();//Tjzb is an instance of a subform, and It's a place to change.Tjzb. MdiParent = This; Tjzb. Show (); } Else { This. Mdichildren[index]. Show (); } }
WinForm A continuous Click button to open a form only once