This post comes from a few queries about a subject that can be confusing as the mechanism for communication of events between the controls and their respective consumers. Here we see an example of how to build a custom control in c # with visual studio 2005 and how to add events to study in detail the mechanism of communication between the controls that publish events and subscribers who use such tests.
events model C # has its roots in the programming model event that is popular in asynchronous programming. The basic founding principle of this model is one in which we have publisher and subscriber, in this model have a certain logic elements by publishing an event. They then send the event fired only to subscribers who are subscribed to the event.
In C # any object can publish a set of events that applications can subscribe. When the object fires the event which publishes all applications must be notified subscribers. In the picture you can see an outline of this operation:
EVENTS AND DELEGATES
In the heart of events in C # are the delegates. When an object generates an event this must be sent "out." These events are shipped out through the use of delegates.
the event declaration in C # is performed as follows:
[attributes] [modifiers] event type member-name;
type in this Declaration may be a delegate. Strictly speaking the delegate can be any delegate declared legally to the language. But there is a convention that is often followed and is the same as that used in Windows Form. By convention, the delegate accepts two parameters:
the event declaration in C # is performed as follows:
[attributes] [modifiers] event type member-name;
type in this Declaration may be a delegate. Strictly speaking the delegate can be any delegate declared legally to the language. But there is a convention that is often followed and is the same as that used in Windows Form. By convention, the delegate accepts two parameters:
- The object generating the event
- Parameters for the specific event
Here we see some examples of this:
public delegate void SubmitClickedHandler (object sender, EventArgs e);
public event SubmitClickedHandler SubmitClicked ;
Now an example of how the whole mechanism in conjugated nt, for that we first create a simple user control as follows:
Here we see the commented code that implements the event with their respective control
code below the button click event OK that fired the event
// Declare delegate for OK button clicked.
//
// Most action events (like the Click event) in Windows Forms
// use the EventHandler delegate and the EventArgs arguments.
// We will define our own delegate that does not specify parameters.
// Mostly, we really don't care what the conditions of the
// click event for the OK button were, we just care that
// the Submit button was clicked.
public delegate void SubmitfullNameClickedHandler ();
private string _fullName ;
public string fullName
{
get { return textBox1 . Text + " " + textBox2 . Text . ToUpper (); }
}
// Declare the event, which is associated with our
// delegate SubmitClickedHandler().
public event SubmitfullNameClickedHandler SubmitfullNameClicked ;
// Add a protected method called OnSubmitClicked().
// You may use this in child classes instead of adding
// event handlers.
public virtual void OnSubmitfullNameClicked ()
{
// If an event has no subscribers registerd, it will
// evaluate to null. The test checks that the value is not
/ / null, Ensuring That There Are subsribers Before
/ / calling the event itself.
if ( SubmitfullNameClicked ! = null) {
SubmitfullNameClicked ();}
private void button1_Click ( object sender , EventArgs e )
{
if ( textBox1 . Text == string . Empty && textBox2 . Text == string. Empty ) {
this . _fullName = string. Empty ;
} else
OnSubmitfullNameClicked ();}
Thus we see how they relate to events from user controls and delegates.
In the following example we see the implementation other user control, in this case we have the same elements as the previous control ie 2 textbox and a button, but in this case we will take each of the textbox the user name and password and then make a some validation of these types of user credentials which triggers one of the two events is my new control, or logInFailed logInSuccess see how it implements the code of this control
In the following example we see the implementation other user control, in this case we have the same elements as the previous control ie 2 textbox and a button, but in this case we will take each of the textbox the user name and password and then make a some validation of these types of user credentials which triggers one of the two events is my new control, or logInFailed logInSuccess see how it implements the code of this control
private string _userName ;
private string _passWord ;
private string userName
{
get { return textBox1 . Text ; }
}
private string passWord
{
get { return textBox2 . Text ; }
}
public delegate void EventHandler ( Object sender , EventArgs e );
public event EventHandler logInSuccess ;
public event EventHandler logInFailed ;
private bool check ( string UserName , string PassWord )
{
//Add here all validation logic
//I made a trivial logic to validate password=1234
if ( passWord == "1234" )
return true ;
return false;}
First we see a couple of properties where you store your user name and password you put into the TextBox control. Then look at the declaration of our delegate that will allow us to use our events, in this case the delegate's statement coincides with the traditional convention used in windows forms.
Then we declare as the two events named above. For the final we have a method that is the logic that implements (in this case trivial) validation user credentials. Below we see the code for the OK button click event:
Then we declare as the two events named above. For the final we have a method that is the logic that implements (in this case trivial) validation user credentials. Below we see the code for the OK button click event:
private void button1_Click ( object sender , EventArgs e )
{
if ( check ( textBox1 . Text , textBox2 . Text ))
{
if ( logInSuccess != null )
logInSuccess ( this , new EventArgs ());
} else {
if ( logInFailed ! = Null )
logInFailed ( this , new EventArgs ());
}}
Here we see the moment when events are triggered in the control, click the OK button. Below is the code of an application that consumes our control.
I hope these two examples will have been useful to better understand how the events in C #. Greetings and till the next.
public partial class Form2TexstLogInCustomControl : Form
{
public Form2TexstLogInCustomControl ()
{
InitializeComponent ();
}
private void Form2TexstLogInCustomControl_Load ( object sender , EventArgs e )
{
label1 . Text = string . Empty ;
}
private void logInBox1_logInFailed ( object sender , EventArgs e )
{
label1 . Text = string . Empty ;
label1 . Text = "LOG IN FAILED" ;
}
private void logInBox1_logInSuccess ( object sender , EventArgs e )
{
Label1 . Text = string. Empty ;
label1. Text = "LOG IN SUCCESS" ;
}}