Using custom events to implement communication between different forms-C # Articles
Source: Internet
Author: User
An "event" in C # is a way for a class to provide notification to customers of that class when something happens when the object satisfies a certain condition. With events, the object owning the event does not have to know who needs to be notified, and once a condition is met, the event is automatically invoked to correctly notify each object that needs to be notified. By using events, the degree of modularity of the program is improved.
To use custom events in your program, follow these steps:
1. Declaring events
To declare an event within a class, you must first declare the delegate type of the event:
public delegate void SelectionChangedEventHandler (object sender,
SelectionChangedEventArgs e);
Note: The EventArgs derived class SelectionChangedEventArgs is defined because the sample program needs to pass data in events.
public class Selectionchangedeventargs:eventargs
{
private string m_selection;
This property is used to pass event data
public string Selection
{
get {return m_selection;}
}
Public SelectionChangedEventArgs (String selection)
{
M_selection = Selection;
}
}
A delegate type defines a set of parameters that are passed to the method that handles the event. Multiple events can share the same delegate type, so you need to perform this step only if you have not yet declared any appropriate delegate types.
Next, declare the event itself:
public event SelectionChangedEventHandler SelectionChanged;
2. Invoke Event
If no client hooks the delegate to the event, the field is empty, otherwise the field references the delegate that should be invoked when the event is invoked. Therefore, when an event is invoked, it is usually checked for empty before the event is invoked.
public class Form2:System.Windows.Forms.Form
{
... ...
public event SelectionChangedEventHandler SelectionChanged;
... ...
private void ComboBox1_SelectedIndexChanged (object sender, System.EventArgs e)
{
if (selectionchanged!= null)
{
SelectionChangedEventArgs E = new SelectionChangedEventArgs (combobox1.text);
SelectionChanged (this, E);
}
}
}
3, linked to the event
To begin receiving an event call, the client code first creates a delegate of the event type that references the method that should be invoked from the event. It then uses + + to write the delegate to any other delegate that the event might connect to.
public class Form1:System.Windows.Forms.Form
{
... ...
Private Form2 M_frm2;
... ...
Public Form1 ()
{
... ...
M_frm2 = new Form2 ();
Hooking up with events
M_frm2. SelectionChanged + = new SelectionChangedEventHandler (frm2_selectionchanged);
}
private void Frm2_selectionchanged (object sender, SelectionChangedEventArgs e)
{
Get the data that the event passes over
TextBox1.Text = e.selection;
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service