Delegation and Event Use in CS - Taking Cross Form Passing Value in Winform as an Example

scene

Delegate

Delegate is a reference type variable that holds a reference to a method.

Delegates are specifically used to implement events and callback methods.

Statement of Entrustment

public delegate int MyDelegate (string s);

 

Instance delegation

The delegate type is declared, and the delegate object must be created using the new keyword, which is related to a particular method.

When a delegate is created, the parameters passed to the new statement are written like method calls, but without parameters. For example:

public delegate void printString(string s);
...
printString ps1 = new printString(WriteToScreen);

 

Sample code

using System;

delegate int NumberChanger(int n);
namespace DelegateAppl
{
   class TestDelegate
   {
      static int num = 10;
      public static int AddNum(int p)
      {
         num += p;
         return num;
      }

      public static int MultNum(int q)
      {
         num *= q;
         return num;
      }
      public static int getNum()
      {
         return num;
      }

      static void Main(string[] args)
      {
         // Create a delegate instance
         NumberChanger nc1 = new NumberChanger(AddNum);
         NumberChanger nc2 = new NumberChanger(MultNum);
         // Calling methods using delegated objects
         nc1(25);
         Console.WriteLine("Value of Num: {0}", getNum());
         nc2(5);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}

 

Event

Events are declared and generated in classes, and are associated with event handlers by using delegations in the same class or other classes. Classes containing events are used to publish events. This is called Publisher class. Other classes that accept this event are called subscriber classes. Event use publisher-subscriber Model.

publisher Is an object that contains event and delegate definitions. The relationship between events and delegates is also defined in this object. Objects of the publisher class call this event and notify other objects.

subscriber Is an object that accepts events and provides event handlers. Delegates in the publisher class call methods in the subscriber class (event handlers).

Declaring events

To declare an event within a class, you must first declare the delegate type of the event.

public delegate void BoilerLogHandler(string status);

 

Then declare the event itself.

public event BoilerLogHandler BoilerEventLog;

 

Examples of value passing across forms

Effect

 

 

 

Realization

First declare delegation in Curve Compare

 public delegate void ChangeTextDelegete(string s);

 

Then define events based on this delegation

 public static event ChangeTextDelegete changeBoxTextEvent;

Next is the handling of the button's specific click event.

 private void button1_Click(object sender, EventArgs e)
        {
            string aa = "hello";
            if(changeBoxTextEvent != null){
                changeBoxTextEvent(aa);
            }
        }

 

Complete Bullet Form Code

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

namespace ZedGraphTest
{
    public partial class CurveCompare : Form
    {
        //Definition of delegation
        public delegate void ChangeTextDelegete(string s);
        //Event Statement
        public static event ChangeTextDelegete changeBoxTextEvent;
        public CurveCompare()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string aa = "hello";
            if(changeBoxTextEvent != null){
                changeBoxTextEvent(aa);
            }
        }

       
    }
}

 

Then go to the form where you want to change the value.

In its initialization method

 CurveCompare.changeBoxTextEvent += new CurveCompare.ChangeTextDelegete(changeText);

 

Then write a way to change the value

 public void changeText(string s)
        {
            this.textBox1.Text = s;
        }

 

Form1 complete sample code

 public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
          
            CurveCompare.changeBoxTextEvent += new CurveCompare.ChangeTextDelegete(changeText);
        }

        public void changeText(string s)
        {
            this.textBox1.Text = s;
        }

 

Keywords: C# Windows

Added by quadlo on Fri, 30 Aug 2019 06:00:30 +0300