C# WINFORM passes values ​​(custom event parameters) between delegates and event forms (cross-forms)-detailed examples

C# WINFORM passes values ​​(custom event parameters) between delegates and event forms (cross-forms)-detailed examples

C# WINFORM passes values ​​(custom event parameters) between delegates and event forms (cross-forms)-detailed examples

In the daily development of C# WINFORM, we usually need to encounter the problem of passing values ​​across forms, and there are many ways to implement them. Today, I will share with you a way of passing values ​​through delegation and events. Not much nonsense, I will use a specific example to show you how to use very delegates and events and custom parameters to achieve cross-form value transfer.

For example: in the program we have a main form, where the value of the text box is what we need to get from another form

In this form, after filling in the complete contact address information, we hope to send this information back to the main form

Okay, the effect has been shown, let me show you the source code below.

The first is the assignment form, as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DelegatePassValue
{
  public partial class frmAddress: Form
  {
   //Declare a delegate to update Address
    public delegate void AddressUpdateHandler(object sender, AddressUpdateEventArgs e);

   //Declare an event to update Address
    public event AddressUpdateHandler AddressUpdated;

    public frmAddress()
    {
      InitializeComponent();
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
      var args = new AddressUpdateEventArgs(txtCountry.Text, txtState.Text, txtCity.Text, txtZipCode.Text);
      AddressUpdated(this,args);
      this.Dispose();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Dispose();
    }
  }

  public class AddressUpdateEventArgs: System.EventArgs
  {
    private string mCountry;
    private string mState;
    private string mCity;
    private string mZipCode;
    public AddressUpdateEventArgs(string sCountry, string sState, string sCity, string sZipCode)
    {
      this.mCountry = sCountry;
      this.mState = sState;
      this.mCity = sCity;
      this.mZipCode = sZipCode;
    }
    public string Country {get {return mCountry;}}
    public string State {get {return mState;}}
    public string City {get {return mCity;}}
    public string ZipCode {get {return mZipCode;}}
  }
}

Secondly, the main form (that is, the form to obtain the passed value), as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DelegatePassValue
{
  public partial class frmMain: Form
  {
    public frmMain()
    {
      InitializeComponent();
    }

    private void btnSetAddress_Click(object sender, EventArgs e)
    {
      var frmAddr = new frmAddress();
      frmAddr.AddressUpdated += new frmAddress.AddressUpdateHandler(AddressForm_ButtonClicked);
      frmAddr.Show();
    }

    private void AddressForm_ButtonClicked(object sender, AddressUpdateEventArgs e)
    {
      txtCountry.Text = e.Country;
      txtState.Text = e.State;
      txtCity.Text = e.City;
      txtZipCode.Text = e.ZipCode;
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
      Application.Exit();
    }
  }
}

Well, here is the end of this example--C# WINFORM form through delegation and event passing values ​​(custom event parameters) implementation is over, if you have better suggestions or comments on the content shared on this site, welcome to I leave a message, I hope we can learn and make progress together, thank you.

Finally, if you are interested in the source code of this example program, you can click here to download

This article is synchronously published to Wangsharings.com [ http://2sharings.com ]: C# WINFORM forms pass values ​​through delegates and events (custom event parameters)--Detailed examples

Reference: https://cloud.tencent.com/developer/article/1034521 C# WINFORM passes values ​​(custom event parameters) between delegates and event forms (cross-forms)-Detailed examples-Cloud + community-Tencent Cloud