Event Procedure in Windows Forms Application

What happens when we double-click on an event of a control in the property window?

When we double-click on an event in the property window, internally a method is generated for writing the code, this method has the special name "Event Procedure" that is a block of code bound with an event of the control and is executed whenever the event occurs.

The code written under the event procedure will be executed by the event when it occurs taking the help of a delegate internally as in the following:



In the preceding case whenever the event occurs it will call the delegate that then executes the event procedure bound to the event.

Because a delegate is responsible for execution of the event procedure, first the event, the delegate and the event procedure will be bound with each other as in the following:

Syntax

<control>.<event>+=new <delegate>(<event proc>)

Example

this.Load += new EventHandler(Form1_Load);

button1.Click +=
new EventHandler(button1_Click);

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

Events and Delegates are predefined under BCL (events are defined in control classes and delegates are defined under namespaces). What is defined here is only an event procedure.

After defining the event procedure in the form class Visual Studio links the Event, Delegate and Event Procedure as we saw above, that can be found under the method InitializeComponent.

Note: 1 delegate can be used by multiple events to execute event procedures, but all events will not use the same delegates where different events may use a different delegate to execute event procedures.

Defining an Event Procedure Manually

To define an Event procedure manually we need to adopt a standard format as in the following.

Syntax

[<modifiers>] void <Name>(object sender, EventArgs e)
{
<Stmts>;
}

Event procedures do not return a value.

An event procedure can have any name but Visual Studio has a convention for naming event procedures, <control name>_<event>.

Example: Form1_Load, button1_Click, textBox1_KeyPress

Note: The concept of events and event procedures has been derived from the classical VB language., but there an event procedure can be bound with only a single event of a single control, whereas in .NET it can be bound with multiple events of a single control as well as with multiple controls also.

Note: Images are taken from Google.

Now here I demonstrate binding an event procedure with multiple events of a control.

  1. Open Visual Studio
  2. Select "File" -> "New" -> "Project..."



  3. Select the language as “Visual C#” and template as “Windows Forms Application” and provide an appropritate name; I used the name "BindControl". Select a location then click on "OK".



  4. Then the Form will be opened and look like this.



  5. Drag and drop controls, like a TextBox and a Button, onto the form.



  6. Double-click on any one control; I clicked on the button1 control so the button1_Click event procedure is generated. Write the code inside. We need to check whether or not either control is bound successfully so I wrote the following.

    Message box to display Message.



    If you want to learn about event procedures in details the read my article.
  7. Bind all the remaining controls, Form1, TextBox1 like in the following to an event procedure then select an event and select a dropdown for the event procedure that you want to bind to.



  8. Now save the application.and run it. Select any control; they will show the same message because we bind all controls with the same event procedure.


X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now