Monday, August 25, 2008

Building Custom Controls

Besides the standard controls we may wish to create our own controls. For e.g. the existing controls might not fulfill our application�s needs. So these are nothing but user-defined controls.

We plan to make a user-defined timer control. The timer will just show the current date and time in six textboxes. Two functions will be provided to start and stop the timer.

To create a Custom Control, create a new Windows Control Library project in VS.NET:

Click on OK and a form will be created which would be derived from UserControl class.

Drag in 6 textboxes each for day, month, year, hour, minute and second and four labels two with text �/� and two with text �:�. This is how it should look after adding the controls.

All the text boxes should be made read-only. Intuitively name them as day, month, year, hour, min and sec. Also add one bool variable st, which denotes the status of timer, whether it is on or off. To stop the timer this variable is set to true and to start it this variable is set to false.

To this class we will add 3 functions viz: - settime( ), start( ) and stop( ).

To start the timer following code should be written

public void start ( )
{

Thread t = new Thread ( new ThreadStart ( settime ) ) ;
st = false ;
t.Start ( ) ;

}

Here a new Thread t is started whose delegate function is settimer( ). Before starting the Thread the st variable is set to false. Stopping the timer is as simple as:

public void stop ( )
{

st = true ;

}

In the settimer( ) function we set the Text properties of all the textboxes to Current Date and Time using the DateTime class. We made the thread wait for 100 milliseconds and kept on updating the textboxes with the appropriate values until st was made to be true, This could happen only if the stop function was called.

public void settime ( )
{

while ( ! st )
{

day.Text = DateTime.Now.Day.ToString ( ) ;
month.Text = DateTime.Now.Month.ToString ( );
year.Text = DateTime.Now.Year.ToString ( ) ;
hour.Text = DateTime.Now.Hour.ToString ( ) ;
min.Text = DateTime.Now.Minute.ToString ( ) ;
sec.Text = DateTime.Now.Second.ToString ( ) ;
Thread.Sleep ( 100 ) ;

}

}

Now we need to build and not compile the code. This will create the mycontrol.dll file.


Client

To use this control we have to create a client for it. To make a client, create a Windows Application project and name it as client. To add the custom control to the toolbox right click in the Toolbox area, and select Customize Toolbox. Select the .Net Framework Components tab, and then the browse button. Add in the dll we just created. After this gets added, we can drag and drop it in our form. We will add two buttons to start and stop the timer.

The start_click( ) and stop_click( ) look like this:

private void start_Click (object sender, System.EventArgs e )
{

mytimer.start ( ) ;

}

private void stop_Click ( object sender, System.EventArgs e )
{

mytimer.stop ( ) ;

}

And at any instance of time when we press start we should get the current time and date as shown:

No comments: