Tuesday, February 25, 2014

Properties & Events in Windows Application

Properties

As we are aware that every control has Properties, Method & Events to them. To access the properties of event Visual Studio provides property window, which will list all the properties and events corresponding to a control. To open it select the control and press F4, which will by default list the properties of the control, change any property you want like left, right, width, height, back color etc. for which you can see the impact immediately.

- To view events of control select events option on top of the property window.
- Methods of a control can be accessed only in CODE view.
- Whenever we set a value to any property of the control VS will internally write the necessary code referring to each property of control by assigning values, what we specify in property window. That code will be present in initialize component method, which is called from the constructor.

Eg: 
this.Text = “ First Form”;
this.BackColor = Color.Pink;
this.Size = new Size(350,350);



Note – To view the code under initialize component method right click on it --> select got definition, which takes you to a partial class, under witch the method was defines  (Form1.Designer.cs)

Events

These are time periods which tells when an action has to be performed i.e. when we want to perform any action at a specific time period choose an appropriate event , that occurs at the required time in the property window and dabble click on it . That will take you to code view and provide a method under which we need to write the code.

In the project we have opened --> got properties of form --> select events and double click on load event  --> and write the following code under form1_load method generated in code view.
- MessageBox.Show(“Welcome to windows app”)

Again go to design view select events and double click on click event, the write following code under the method Form1_click
- MessageBox.Show(“you have click on form”)

Que. What is the method under which we are writing the code corresponding to an event?
Ans. The method under which we are writing the code corresponding to an event has a special name i.e. event procedure, which is a block of code i.e. bound with an event of control and gets executed whenever the event occur or rises . The code written under an event procedure will be internally executed by the event whenever it occurs or rises with the help of a delegate.


- In the above case whenever the event occurs or rises will call the delegate, which then executes the event procedure i.e. bound with event.

Que. How does an event, delegate and event procedure gets linked with each other?
Ans. Events and delegates are predefined under BCL (events are defined in control classes and delegates are defined under system namespace) , what we defined here is only an event procedure. After defining an event procedure in form class to link the event, delegate and event procedure VS write a mapping statement as following:

Syntax : < control >.< event >+= < delegates > ( < event procedure  > )

Eg:
          this.Load += new EventHandler(Form1_Click);
          button1.Click += new EventHandler(button1_click);
          textBox1.keyPress += new KeyPressEventHnandler(textBox1_KeyPress);

Multiple events can used the same delegate but all events will not use the same delegate. Different event may used different delegates to execute an event procedure.

Note- we can see the above code under the method InitializedComponent.

Que. How can we define an event procedure manually.
Ans. We can define event procedures manually as following:

Syntax: 

[ < Modifier > ] void < name > ( object sender, Event Args e)
{
         <  stmts >;
}
- Events procedure are non value returning methods.

- We can give any name to a Event procedure, but VS follows a convention wile naming them:

< Control name > _ < event >
Eg:       Form_Load,
            Button1_Click,
            textBox1_keypress
           
- Every event procedure should take to mandatory parameters:

1. Object sender.
2. EventArgs e

- Write the blow code in a Notepad and execute:
// I don't know why it shows error but main thing is the concept.
// So don’t copy &amp; past this , and if you are able to run , then please give me this solution

using System;
using System.Drawing;
using System.Windows.Forms;
public class MyForm : Form
{
    public MyForm2()
   {
       InitializeComponent();
   }
     private void InitializeComponent();
   {
           this.Text =”My Form”;

       this.BackColor = Color.Pink;
       this.Size = new Size(350,350);
      this.Load += new Eventhandler(Test);
      this.Click += new Eventhandler(Test);
      
    }
private void Test(object sender, EventArgs e)
{
  MessageBox.Show(“Procedure written manually”);
}
static void Main()
{
    Application.Run(new MyForm2());;

}
}

Note- This concept of events &amp; event procedure’s has been derived from classical VB , but them an event procedures can be bound only single event of single control, whereas in .net it can be bound with multiple events of a single control os well as with multiple control also.

No comments: