We encounteredWinFormInteraction between formsHere we will summarize the various methods. The content is relatively basic and suitable for beginners who are just getting started. Let's take a look at it!
There are many ways to pass data between forms:
1. Define a constructor in the subform. The parameter type is the main form. to display the subform, use this constructor to instantiate the subform, then pass the this pointer in. It is too abstract. I should understand it as soon as I write it:
PublicclassfrmMain: Form
{
...
FrmControlcontrolForm = newfrmControl (this );
ControlForm. Show ();
}
PublicclassfrmControl: Form // subform, used to control some display of the main Form!
{
PrivatefrmMainmainForm;
PublicfrmControl (frmMainmainForm)
{
This. mainForm = mainForm;
}
Privatevoidbutton#click (objectsender, EventArgse)
{
FrmMain. textBox1.Text = this. textBox1.Text; // pass the text box value of the subform to the text box of the main form!
}
}
2. I personally feel that the above method is not very good. Although it is very simple to implement, I just want to change the title Text of the form and pass the reference of the entire main form to the subform, this method is not very elegant. We use interfaces to improve the above method, which can limit the function exposed to subforms and reduce the coupling between forms:
PublicinterfaceIChangeTitle:
{
VoidChangeTitle (stringtitle );
}
PublicclassfrmMain: Form, IChangeTitle
{
...
FrmControlcontrolForm = newfrmControl (this );
ControlForm. Show ();
PublicvoidChangeTitle (stringtitle)
{
This. Text = title;
}
}
PublicclassfrmControl: Form // subform, used to control some display of the main Form!
{
PrivateIChangeTitleichangeTitle;
PublicfrmControl (IChangeTitleichangeTitle)
{
This. ichangeTitle = ichangeTitle;
}
Privatevoidbutton#click (objectsender, EventArgse)
{
IchangeTitle. ChangeTitle (this. textBox1.Text); // call the method through the interface
}
}
3. To further reduce the coupling between forms, we can use delegation to achieve this requirement:
PublicpartialclassChildForm: Form
{
PublicdelegatevoidTitleChangedHandler (stringtitle );
PublicTitleChangedEventHandlerTitleChanged;
PublicChildForm ()
{
InitializeComponent ();
}
Privatevoidbtn_ OK _Click (objectsender, EventArgse)
{
If (TitleChanged! = Null)
TitleChanged ("TestTitle"); // delegate call
}
}
Assign a value to the delegate variable in the main form:
PublicpartialclassMainForm: Form
{
PrivateChildFormloginForm = newChildForm ();
PublicMainForm ()
{
InitializeComponent ();
LoginForm. TitleChanged = newChildForm. TitleChangedEventHandler (FormTitleChanged );
}
ProtectedvoidFormTitleChanged (stringtitle)
{
This. Text = title;
}
Privatevoidbutton#click (objectsender, EventArgse)
{
LoginForm. Show ();
}
}
4. You can also define a custom event in the subform, and then customize an event parameter to pass the information you want to pass:
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) // Method for triggering the event
{
If (TitleChanged! = Null)
TitleChanged (this, e );
}
}
Subscribe to this event on the main form:
PublicpartialclassMainForm: Form
{
PrivateChildFormloginForm = newChildForm ();
PublicMainForm ()
{
InitializeComponent ();
LoginForm. TitleChanged = newChildForm. TitleChangedEventHandler (FormTitleChanged );
}
ProtectedvoidFormTitleChanged (objectsender, ChildForm. TitleChangedEventArgse)
{
This. Text = e. Title;
}
Privatevoidbutton#click (objectsender, EventArgse)
{
LoginForm. Show ();
}
}