In fact, I have also written similar theme in the past. Here I will summarize various methods, and the content is indeed based on some. So this article is written to colleagues who have just learned C, hope to help you! Sorry, there are no strange bugs in this article to arouse everyone's interest, but I will try my best to write some interesting theme in the next article!
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:
Copy codeThe Code is as follows:
Public class frmMain: Form
{
...
FrmControl controlForm = new frmControl (this );
ControlForm. Show ();
}
Public class frmControl: Form // subform, used to control some display of the main Form!
{
Private frmMain mainForm;
Public frmControl (frmMain mainForm)
{
This. mainForm = mainForm;
}
Private void button#click (object sender, EventArgs e)
{
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:
Copy codeThe Code is as follows:
Public interface IChangeTitle:
{
Void ChangeTitle (string title );
}
Public class frmMain: Form, IChangeTitle
{
...
FrmControl controlForm = new frmControl (this );
ControlForm. Show ();
Public void ChangeTitle (string title)
{
This. Text = title;
}
}
Public class frmControl: Form // subform, used to control some display of the main Form!
{
Private IChangeTitle ichangeTitle;
Public frmControl (IChangeTitle ichangeTitle)
{
This. ichangeTitle = ichangeTitle;
}
Private void button#click (object sender, EventArgs e)
{
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:
Copy codeThe Code is as follows:
Public partial class ChildForm: Form
{
Public delegate void TitleChangedHandler (string title );
Public TitleChangedEventHandler TitleChanged;
Public ChildForm ()
{
InitializeComponent ();
}
Private void btn_ OK _Click (object sender, EventArgs e)
{
If (TitleChanged! = Null)
TitleChanged ("Test Title"); // delegate call
}
}
Assign a value to the delegate variable in the main form:
Copy codeThe Code is as follows:
Public partial class MainForm: Form
{
Private ChildForm loginForm = new ChildForm ();
Public MainForm ()
{
InitializeComponent ();
LoginForm. TitleChanged = new ChildForm. TitleChangedEventHandler (FormTitleChanged );
}
Protected void FormTitleChanged (string title)
{
This. Text = title;
}
Private void button#click (object sender, EventArgs e)
{
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:
Copy codeThe Code is as follows:
Public partial class ChildForm: Form
{
Public class TitleChangedEventArgs: EventArgs // event parameter class
{
Private string title = "";
Public string Title
{
Get
{
Return title;
}
Set
{
Title = value;
}
}
}
Public delegate void TitleChangedEventHandler (object sender, TitleChangedEventArgs e );
Public event TitleChangedEventHandler TitleChanged;
Public ChildForm ()
{
InitializeComponent ();
}
Private void btn_ OK _Click (object sender, EventArgs e)
{
TitleChangedEventArgs e1 = new TitleChangedEventArgs ();
E1.Title = "Login sucessed ";
OnTitleChanged (e1); // trigger event
}
Protected virtual void OnTitleChanged (TitleChangedEventArgs e) // Method for triggering an event
{
If (TitleChanged! = Null)
TitleChanged (this, e );
}
}
Subscribe to this event on the main form:
Copy codeThe Code is as follows:
Public partial class MainForm: Form
{
Private ChildForm loginForm = new ChildForm ();
Public MainForm ()
{
InitializeComponent ();
LoginForm. TitleChanged + = new ChildForm. TitleChangedEventHandler (FormTitleChanged );
}
Protected void FormTitleChanged (object sender, ChildForm. TitleChangedEventArgs e)
{
This. Text = e. Title;
}
Private void button#click (object sender, EventArgs e)
{
LoginForm. Show ();
}
}
An event is triggered through the Child window. The parent window implements this event. We can transmit and display data without closing the parent window or child window.
Idea: click "Add User" in the "Add User" subwindow to dynamically trigger an event. Through this event, we can transmit data.
In other windows, you can respond to this event.
The subwindow only triggers an event and delegates the event Implementation Method to other objects. The subwindow does not care about how to handle the event, this reflects a loosely coupled setting.
Knowledge points involved:
How to define an event?
Copy codeThe Code is as follows:
Public delegate void UserAddEventHandler ();
Public event UserAddEventHandler UserAdd;
. How to respond to the events triggered by the subwindow?
UserDialog. UserAdd + = userDialog_UserAdd; // delegate the implementation of UserAdd to the userDialog_UserAdd Method
Void userDialog_UserAdd ()
{
// Method body
}
How do I transmit data to the parent window in the Child Window?
In the delegate declaration, add two parameters:
Public delegate void UserAddEventHandler (object sender, UserEventArgs e );
UserEventArgs is a class inherited from EventArgs.
Public class UserEventArgs: EventArgs
{
Public User currentUser {get; set ;}
Public UserEventArgs (User user)
{
This. currentUser = user;
}
}
Therefore, when an event is triggered, the User information is passed out through the second parameter.
Private void button#click (object sender, EventArgs e)
{
// Trigger an event outward
If (UserAdd! = Null)
{
User user = new User ();
User. UserName = textBox3.Text;
User. PassWord = textBox4.Text;
UserAdd (this, new UserEventArgs (user ));
}
}
In the userDialog_UserAdd method of the parent window, you can process the received data.
Private void Add User ToolStripMenuItem_Click (object sender, EventArgs e)
{
FrmUser userDialog = new FrmUser ();
// Delegate the implementation of UserAdd to the userDialog_UserAdd Method
UserDialog. UserAdd + = userDialog_UserAdd;
UserDialog. ShowDialog ();
}
Void userDialog_UserAdd (object sender, UserEventArgs e)
{
MessageBox. Show (e. currentUser. UserName );
}