Date 4/2/11 Friday
- Add a new form in a project:
- IF we want
one more form in the project we can add it from the add new item window, choosing
the item template Windows Form. Once a new form is added to run the form we
need to specify the form class name, under Application.Run method with in the
class program.
EG:
Application.Run(new
Form2());
|
- Binding an
event procedure with multiple events of a control under VS.
- In the new
form asses --> goto properties of it and double click on load event, which
defines an event procedure Form2_Load --> Now again Goto events of form
select Click event and choose Form2_Load event procedure under it.
- Now write
the following code under event procedure, which gets executed for both Load and
Click events.
Eg:
MessageBox.Show(“Bound with multiple events
of a control”)
|
- Now add a
new form, Form3 in project.
-
We
have provided with number of controls for developing our application, where
each control is define as a class.
-
These
controls are avilabel under the tool box window arranged as groups (tabs).
-
To
place a control of the form either choose your control and double click on it
or select a control and place on the form so that the control is placed at that
location.
Note – use the layout tool bar for aligning of
the controls.
- In form Form3 under project place
three buttons on it.
- Goto
events of button one double click on click event, which defines button1_Click
event procedure, bind the event procedure with click event of button2, button3
and Form also.
- Then tried
the following code under button1_Click event procedure.
if (sender.GetType().Name == "Button")
{
Button
b = (Button)sender;
if
(b.Name=="button1")
MessageBox.Show("Button1 is clisked");
else
if (b.Name=="button2")
MessageBox.Show("Button2 is clisked");
else
if (b.Name=="button3")
MessageBox.Show("Button3 is clisked");
}
else
MessageBox.Show("Form is clicked");
|
Note - Goto the notes of inheritance , read
the 2,3 and 4 rule , you can understand clearly , what we are doing.
- When a
event procedure is bound with multiple controls, the object of the control,
which has raised the event is sent to event procedure in run time and that get
captured under sender parameter of event procedure.
- As a
sender is of type object it can hold instance (or object) of any control that
has raised the event which is sent to the event procedure in run time as
following:
- in the
above case we have bound the event procedure with diff type of controls (button
and form) so first to identify the type of control it was used the get type
method of object class and recognize the type of control (button or form),
raised in the event.
- after
recognizing the type of control it was if there are multiple controls of same type
then we need to convert sender into the appropriate control type to identify
the name of each control, because “sender” is of parent type it can not referred
to members of it child class(button now).
- Converting
sender into appropriate control type can be done in two ways as following:
Button
b = (Button)sender;
or
Button
b = sender as Button;
|
0 comments:
Post a Comment