Pages

Thursday, February 17, 2011

17/02 developing a control:


Date 17-2-11 Thursday

- People working on controls or classified into two Categories:
1. Component Developer
2. Component Consumer

- The person who develops a control is a component developer and who consume the controls is a component consumer.
While developing component the developer of the control should define all the behavior required for the control and also defined any required properties, methods and event to the control.

- Properties are defined to provide access for any value of the control to consumers.
Eg: Text property for of TextBox, Checked property of CheckBox etc.

- Method is defined so that the controls can perform actions whenever required.
Eg: Clear(), Focus(), Methods of textbox
Close() method of Form etc.

- Events
While developing a control the developer of the control may not know what action has to be performed at the specific time period.

Eg: the developer of the control is not aware what should happen when button is clicked, what happen when button is clicked will be decided by component consumer even if decided by consumer, it is the responsibility of developer to execute the code written by consumer even if these two persons never come together to work.

To resolve the above problem developer must first define an event under their controls and then asked the consumer to write code under an event procedure which should be bound with the event, So whenever the event occur, event procedure gets executed.

Define an event:

[ < modifier >] event < delegate > <name>

As we know that events takes the help of delegates to execute an event procedure, so while defining an events we must also specified which delegate will execute the event.


So first we need to define a delegate and then the event

public delegate void EventHandler(object sender EventArgs e);

public event EventHandler click;

- All delegates that are predefined in BCL are defined with two parameters:

1. Object sender
2. EventArgs e

- That is the reason why all our event procedure are also taking the same two parameters ( we are already aware that I/O parameter of delegate should be same ass the I/O prams of method it has to call)

Note à While defining our own event to our controls we can define delegates with or without parameters so that the event procedure that are generated will also come with or without parameters respectively.

Create a StopClock control:

à Open a new project of type windows Forms Control library and name it as controls project.
à By default the project comes with a class userControl1 that is inherited from user control
à Open the Solution explorer and rename the file UserControl1.cs as StopClock.cs and design it as following.

Now goto the properties of Mask TextBox à select Mask property à click on the button beside it  à select time à click ok à and also set text as “0000”

Place a timer control and set its interval property as 1000



Now write the following code in code view

** Code Can not be displayed..


Now open the solution explorer à right click on the project à select built, which compile the project and generated an assembly controlsProject.dll

Consuming the Controls è To consume the controls à goto our old project à open the toolbox à right click on it à and select add tab, which adds a new tab à enter the name as csharp4  controls.
Now right click on the new tab and select choose items, which opens a dialog box à click on the browser button and select controlsProject.dll from its physical location.

c:\csharp4\controlsProject\Bin\Debug\controlsProject.dll

After selecting the assembly à click on the OK button, which adds StopClock control under the new tab we have added, which can be place on any Form and consume it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 comments:

Post a Comment