Prior to the Event Driven model, programming was procedural, where there would be an entry point for the application, typically known as the main function. From there, the flow of execution could be branched to different modules depending upon conditions such as inputs from the user. In an Event Driven Paradigm, the execution is not necessarily continuous. The statements that will be executed depend on the events. The events could be user generated: Clicking a Button, Closing a Window, Moving the mouse over a contol and so on, or system generated such as elapsing of a timer’s interval.

We write codes corresponding to these events which are executed when ever the event takes place. For example, the following code shows a dialog box with the message “Hello World” when the Show button is clicked:-

private void ShowMessage_Click(object sender, EventArgs e)
{
	MessageBox.Show("Hello World");
}

Notice the words object sender, EventArgs e, these are the parameters being passed along with the event. These parameters are used to determine the state of the control. In the following example, the KeyPressEventArgs being passed along with the event, is used to determine the key that was pressed in a text box.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
	MessageBox.Show(e.KeyChar.ToString());
}
No votes yet.
Please wait...