How to subscribe to custom Form events and subscribe to custom form events
The Window Form class has many attributes/methods and events, among which events belong to a publishing and subscription mode. The subscription and publishing mode defines a one-to-many dependency, allowing multiple subscriber objects to listen to a single subject object at the same time. When the status of the subject object changes, it notifies all subscriber objects so that they can automatically update their status. This mode is especially suitable when an object needs to change other objects at the same time without worrying about how many objects need to be changed. This article will demonstrate how to customize an event (custom event) on the form ):
1. Customize a CustomEventArgs class
Generally, a custom event has a parameter inherited from EventArgs. Here we define a CustomEventArgs, which stores the parameter values through custom fields in the class:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 6 namespace CustomEventsDemo 7 {8 public class CustomEventArgs: EventArgs 9 {10 // The Custom field is used to store the 11 public object Tag; 12 public string Message; 13 public CustomEventArgs () 14 {15 16} 17 public CustomEventArgs (string message, object tag) 18 {19 Message = message; 20 Tag = tag; 21} 22} 23}
2. customize an event
Next, create a FormPublisher form, and use event EventHandler <CustomEventArgs> customEvent; event EventHandler <CustomEventArgs> customSecondEvent and custom Event. Their event parameters are CustomEventArgs. in the form loading event, we trigger two events (this order will affect the subscriber's Event Response sequence when the form is loaded. if we create a form that inherits this FormPublisher, we will see in the event panel of this form:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 10 namespace CustomEventsDemo11 {12 public partial class FormPublisher: Form13 {14 // defines two events 15 public event EventHandler <CustomEventArgs> customEvent; 16 public event EventHandler <CustomEventArgs> TomSecondEvent; 17 public FormPublisher () 18 {19 InitializeComponent (); 20} 21 22 private void FormWithCutomEvent_Load (object sender, EventArgs e) 23 {24 // determine the execution sequence of custom events, inheriting the default order of loading this form's subclass form 25 if (customEvent! = Null) 26 {27 CustomEventArgs customEventArgs = new CustomEventArgs (this. textBox1.Text, "customEvent"); 28 customEvent (this, customEventArgs); 29} 30 if (customSecondEvent! = Null) 31 {32 33 CustomEventArgs customEventArgs = new CustomEventArgs (this. textBox1.Text, "customSecondEvent"); 34 customSecondEvent (this, customEventArgs); 35} 36 37} 38 39 private void button#click (object sender, EventArgs e) 40 {41 42 this. textBox2.AppendText (this. textBox1.Text + "\ r \ n"); 43 // this. textBox1.Text = ""; 44 if (customSecondEvent! = Null) 45 {46 CustomEventArgs customEventArgs = new CustomEventArgs (this. textBox1.Text, "customSecondEvent"); 47 // trigger event 48 customSecondEvent (this, customEventArgs); 49} 50 if (customEvent! = Null) 51 {52 CustomEventArgs customEventArgs = new CustomEventArgs (this. textBox1.Text, "customEvent"); 53 // trigger event 54 customEvent (this, customEventArgs); 55} 56} 57} 58}
3. subscribe to events
The following defines a FormSubscriber form to subscribe to custom events. to define an event of another form, you must instantiate the form first. Otherwise, the call will fail:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 10 namespace CustomEventsDemo11 {12 public partial class FormSubscriber: Form13 {14 FormPublisher form = null; 15 public FormSubscriber () 16 {17 InitializeComponent (); 18 // start two forms 19 form = new FormPublisher (); 20 form. visible = true; 21 // subscribe to event 22 form. customSecondEvent + = form_customSecondEvent; 23 // subscribe to event 24 form. customEvent + = form_customEvent; 25 // instantiate the publishing form and pass it into the second subscription form. Otherwise, you cannot subscribe to 26 FormSubScriber2 from2 = new FormSubScriber2 (form); 27 from2.Visible = true; 28} 29 30 void form_customSecondEvent (object sender, CustomEventArgs e) 31 {32 this. textBox1.AppendText ("Message from Publisher" + e. message + "from" + e. tag + "\ r \ n"); 33} 34 35 void form_customEvent (object sender, CustomEventArgs e) 36 {37 this. textBox1.AppendText ("Message from Publisher" + e. message + "from" + e. tag + "\ r \ n"); 38} 39 40 private void FormSubscriber_Load (object sender, EventArgs e) 41 {42 43} 44} 45}
Another form is also subscribed:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 10 namespace CustomEventsDemo11 {12 public partial class FormSubScriber2: Form13 {14 15 public FormSubScriber2 () 16 {17 InitializeComponent (); 18} 19 public FormSubScriber2 (FormPublisher form) 20 {21 InitializeComponent (); 22 // subscribe to form Custom Event 23 form. customEvent + = form_customEvent; 24} 25 void form_customEvent (object sender, CustomEventArgs e) 26 {27 this. textBox1.AppendText ("Message from Publisher" + e. message + "from" + e. tag + "\ r \ n"); 28} 29 30 private void FormSubScriber2_Load (object sender, EventArgs e) 31 {32 33} 34} 35}