How to transfer values between forms by Winform in C # Development
Today, I would like to share with you the relevant knowledge points about how to transfer values between forms in Winform development. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
I. Preface
When we are developing Winform forms programs, we often encounter forms passing values to each other. Suppose there is a scenario: a main form and a sub-form, click the button on the main form to pass values to the sub-form, and display on the sub-form, there are generally the following ways to achieve.
II. Public attributes
We can define a common property in the sub-form, and then assign a value to the common property in the parent form, so that we can pass values between forms. The sub-form code is as follows:
Using System;using System.Windows.Forms;namespace DelegateDemo {public partial class frmChild: Form {public frmChild () {InitializeComponent ();} / / defines a public property that receives the passed value public string strMessage {get; set } / form load / private void frmChild_Load (object sender, EventArgs e) {/ / display the received value on the form this.lblMessage.Text = strMessage;}
Parent body code:
Using System;using System.Windows.Forms;namespace DelegateDemo {public partial class frmParent: Form {public frmParent () {InitializeComponent ();} / click event / private void btn_Value_Click (object sender, EventArgs e) {frmChild child = new frmChild () / / assign the public property of the form child.strMessage = this.txtMessage.Text.Trim (); / / display the sub-form child.Show ();}
This approach has a drawback: the property needs to be set to public, which is not safe.
III. Public methods
We can also define a method in the sub-form and pass the value by calling the method. The sub-form code is as follows:
Using System;using System.Windows.Forms;namespace DelegateDemo {public partial class frmChild: Form {public frmChild () {InitializeComponent ();} / defines a public property that receives the passed value / / public string strMessage {get; set;} / / defines the attribute as private private string strMessage {get; set } / assign a value to the private attribute / public void SetText (string strText) {strMessage = strText } / form load / private void frmChild_Load (object sender, EventArgs e) {/ / display the received value on the form this.lblMessage.Text = strMessage;}
Parent body code:
Using System;using System.Windows.Forms;namespace DelegateDemo {public partial class frmParent: Form {public frmParent () {InitializeComponent () } / Click event / private void btn_Value_Click (object sender, EventArgs e) {# region call public property assignment / / frmChild child = new frmChild () / assign a value to the public property of the form / / child.strMessage = this.txtMessage.Text.Trim (); / / display the sub-form / / child.Show (); # endregion # region call method assignment frmChild child = new frmChild () / / assign child.SetText (this.txtMessage.Text.Trim ()) to the public property of the form; / / display the sub-form child.Show (); # endregion}
This approach also has its drawbacks: although the property is private, the method is still public.
IV. Entrusting
Both of the above methods are not safe, so let's use a delegate to pass values between forms.
1. Define a delegate
We define a delegate with parameters and no return values in the main form:
/ / define a delegate private delegate void SendMessage (string strMessage) with parameters and no return values; 2. Instantiate an event of this delegate type
Define an event of delegate type in the parent form:
/ / define an event public event SendMessage sendMessageEvent of delegate type
The relationship between delegate and event, event is more secure and less coupled than delegate. A delegate is a type, and an event is an instance of a delegate type.
3. Define the method to be executed
This is actually defining a method to assign a value to the control in the sub-form:
/ public void SetValue (string strValue) {this.lblMessage.Text = strValue;} 4, bind the method to the event frmChild child = new frmChild (); / / bind the method to the event sendMessageEvent + = new SendMessage (child.SetValue); / / you can also use the following abbreviated form / / sendMessageEvent + = child.SetValue;child.Show (); 5. Trigger the delegate
Trigger the delegate in the click event of the button:
If (sendMessageEventures null) {sendMessageEvent.Invoke (this.txtMessage.Text.Trim ());}
The custom delegate is used in the above code, or we can use the Action generic delegate that comes with the .net framework:
Using System;using System.Windows.Forms;namespace DelegateDemo {public partial class frmParent: Form {/ / defines a delegate public delegate void SendMessage (string strMessage) with parameters and no return values; / / defines an event of delegate type public event SendMessage sendMessageEvent; public event Action actionEvent; public frmParent () {InitializeComponent () } / Click event / private void btn_Value_Click (object sender, EventArgs e) {# region call public property assignment / / frmChild child = new frmChild () / assign the public property of the form / / child.strMessage = this.txtMessage.Text.Trim (); / display the sub-form / / child.Show (); # endregion # region call method assignment / / frmChild child = new frmChild () / assign a value to the public property of the form / / child.SetText (this.txtMessage.Text.Trim ()); / display the sub-form / / child.Show (); # endregion # region passes the value / / frmChild child = new frmChild () by delegating / bind a method to an event / sendMessageEvent + = new SendMessage (child.SetValue); / you can also use the following abbreviated form / / sendMessageEvent + = child.SetValue; / / child.Show (); # endregion # region uses Action frmChild child = new frmChild () / / bind the method to the event actionEvent + = child.SetValue; child.Show (); # endregion / / use a custom delegate / / if (sendMessageEventures null) / / {/ / sendMessageEvent.Invoke (this.txtMessage.Text.Trim ()) / / use Action to delegate if (actionEvent! = null) {actionEvent.Invoke (this.txtMessage.Text.Trim ());} above is all the content of the article "how to transfer values between forms by Winform in C# Development". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.