We are in the process of doing a program to encounter the problem of interaction between WinForm forms, here a variety of methods to sum up, the content of the foundation, suitable for beginners, the following we take a look at it!
There are many more ways to pass data between forms:
1, customize a constructor in the subform, the parameter type is the main form, when you want to display the subform, use this constructor to instantiate the subform, and then pass the this pointer into it, it's too abstract to say, and I'll probably write about it:
Publicclassfrmmain:form
{
...
Frmcontrolcontrolform=newfrmcontrol (this);
Controlform.show ();
}
publicclassfrmcontrol:form//Subform To control some of the display of the main form!
{
Privatefrmmainmainform;
Publicfrmcontrol (Frmmainmainform)
{
This.mainform=mainform;
}
Privatevoidbutton1_click (OBJECTSENDER,EVENTARGSE)
{
frmmain.textbox1.text=this.textbox1.text;//the text box value of the handle form to the text box of the main form!
}
}
2, I personally feel that the above method is not very good, although the implementation is very simple, just want to change the title text of the form, the whole main form of the reference to pass to the subform, this way is not very elegant, we use the interface to improve the above method, so that you can limit the functionality exposed to the subform, reduce the coupling between forms:
Publicinterfaceichangetitle:
{
Voidchangetitle (Stringtitle);
}
Publicclassfrmmain:form,ichangetitle
{
...
Frmcontrolcontrolform=newfrmcontrol (this);
Controlform.show ();
Publicvoidchangetitle (Stringtitle)
{
This. Text=title;
}
}
publicclassfrmcontrol:form//Subform To control some of the display of the main form!
{
Privateichangetitleichangetitle;
Publicfrmcontrol (Ichangetitleichangetitle)
{
This.ichangetitle=ichangetitle;
}
Privatevoidbutton1_click (OBJECTSENDER,EVENTARGSE)
{
Ichangetitle.changetitle (This.textBox1.Text);//calling method via interface
}
}
3, to further reduce the coupling between forms, we can use delegates to achieve this requirement:
Publicpartialclasschildform:form
{
Publicdelegatevoidtitlechangedhandler (Stringtitle);
publictitlechangedeventhandlertitlechanged;
Publicchildform ()
{
InitializeComponent ();
}
Privatevoidbtn_ok_click (OBJECTSENDER,EVENTARGSE)
{
if (titlechanged!=null)
Titlechanged ("Testtitle");//Delegate invocation
}
}
The main form assigns values to the delegate variable:
Publicpartialclassmainform:form
{
Privatechildformloginform=newchildform ();
Publicmainform ()
{
InitializeComponent ();
Loginform.titlechanged=newchildform.titlechangedeventhandler (formtitlechanged);
}
Protectedvoidformtitlechanged (Stringtitle)
{
This. Text=title;
}
Privatevoidbutton1_click (OBJECTSENDER,EVENTARGSE)
{
Loginform.show ();
}
}
4, you can also define a custom event in the subform, and then customize an event parameter to pass some of the information you want to deliver:
Publicpartialclasschildform:form
{
publicclasstitlechangedeventargs:eventargs//Event Parameter class
{
Privatestringtitle= "";
Publicstringtitle
{
Get
{
Returntitle;
}
Set
{
Title=value;
}
}
}
Publicdelegatevoidtitlechangedeventhandler (OBJECTSENDER,TITLECHANGEDEVENTARGSE);
publiceventtitlechangedeventhandlertitlechanged;
Publicchildform ()
{
InitializeComponent ();
}
Privatevoidbtn_ok_click (OBJECTSENDER,EVENTARGSE)
{
Titlechangedeventargse1=newtitlechangedeventargs ();
E1. Title= "loginsucessed";
Ontitlechanged (E1);//Trigger Event
}
Protectedvirtualvoidontitlechanged (TITLECHANGEDEVENTARGSE)//Trigger event method
{
if (titlechanged!=null)
Titlechanged (this,e);
}
}
The main form is subscribed to this event:
Publicpartialclassmainform:form
{
Privatechildformloginform=newchildform ();
Publicmainform ()
{
InitializeComponent ();
Loginform.titlechanged =newchildform.titlechangedeventhandler (formtitlechanged);
}
Protectedvoidformtitlechanged (OBJECTSENDER,CHILDFORM.TITLECHANGEDEVENTARGSE)
{
This. Text=e.title;
}
Privatevoidbutton1_click (OBJECTSENDER,EVENTARGSE)
{
Loginform.show ();
}
}