search c#.net

Chat With Me

คลิ๊กๆๆ...

Monday, March 16, 2009

Inheriting from a User Control (c#.net)

In the previous section, you learned how to combine Windows controls, components, and code into reusable user controls. Your user control can now be used as a base upon which other controls can be built. The process of deriving a class from a base class is called inheritance. In this section, you will create a user control called ctlAlarmClock. This control will be derived from its parent control, ctlClock. You will learn to extend the functionality of ctlClock by overriding parent methods and adding new methods and properties.

Creating the Inherited Control

The first step in creating an inherited control is to derive it from its parent. This action creates a new control that has all of the properties, methods, and graphical characteristics of the parent control, but can also act as a base for the addition of new or modified functionality.

To create the inherited control

  1. In Solution Explorer, click ctlClockLib.
  2. From the Project menu, select Add Inherited Control.

    The Add New Item window opens with Inherited User Control selected.

  3. In the Name box, type ctlAlarmClock.cs, and click Open.

    The Inheritance Picker window appears.

  4. Under Component Name, double-click ctlClock.
  5. In Solution Explorer, browse the current projects. Note that a file called ctlAlarmClock has been added.

Adding the Alarm Properties

Properties are added to an inherited control in the same way they are added to a user control. You will now use the property declaration syntax to add two properties to your control: AlarmTime, which will store the value of the date and time the alarm is to go off, and AlarmSet, which will indicate whether or not the alarm is set.

To add Properties to your user control

  1. In Solution Explorer, right-click ctlAlarmClock and select View Code.
  2. Locate the public class statement. Note that your control inherits from ctlClockLib.ctlClock. Beneath the { statement, type the following code:
    private DateTime dteAlarmTime;
    private bool blnAlarmSet;
    // These properties will be declared as public to allow future
    // developers to access them.
    public DateTime AlarmTime
    {
    get
    {
    return dteAlarmTime;
    }
    set
    {
    dteAlarmTime = value;
    }
    }
    public bool AlarmSet
    {
    get
    {
    return blnAlarmSet;
    }
    set
    {
    blnAlarmSet = value;
    }
    }

Adding to the Graphical Interface of Your Control

Your inherited control has a visual interface that is identical to the control it inherits from. It possesses the same constituent controls as its parent control, but the properties of the constituent controls will not be available unless they were specifically exposed. You may add to the graphical interface of an inherited user control in the same manner as you would add to any user control. To continue adding to your alarm clock's visual interface, you will add a label control that will flash when the alarm is sounding.

To add the label control

  1. In Solution Explorer, right-click ctlAlarmClock and select View Designer from the shortcut menu.

    The designer for ctlAlarmClock opens in the main window.

  2. Click the display portion of the control, and observe the Properties window.

    Note that while all the properties are displayed, they are dimmed. This indicates that these properties are native to lblDisplay, and cannot be modified or accessed in the properties window. By default, controls contained in a user control are private, and their properties are not accessible by any means.

    Tip If you want subsequent users of your user control to have access to its internal controls, declare them as public or protected. This will allow you to set and modify properties of controls contained within your user control by using the appropriate code.
  3. Add a Label control to your user control.
  4. Using the mouse, move the label control immediately beneath the display box. In the Properties window, set the following properties:












Property.............................Setting

Name........................................................lblAlarm
Text.........................................................alarm!
TextAlign................................................Middle Center
Visible.......................................................Visible

Adding the Alarm Functionality

In the previous sections, you added properties and a control that will enable alarm functionality in your user control. In this section, you will add code to compare the current time to the alarm time, and if they are the same, to flash an alarm. By overriding the timer1_Tick method of ctlClock, and adding additional code to it, you will extend the capability of ctlAlarmClock while retaining all of the inherent functionality of ctlClock.

To override the timer1_Tick method of ctlClock

  1. In the Code Editor, locate the private bool blnAlarmSet; statement. Immediately beneath it, add the following statement:
    private bool blnColorTicker;
  2. In the Code Editor, locate the } at the end of the class.
  3. Just before the }, add the following code:
    protected override void timer1_Tick(object sender, System.EventArgs e)
    {
    // Calls the Timer1_Tick method of ctlClock.
    base.timer1_Tick(sender, e);
    // Checks to see if the Alarm is set.
    if (AlarmSet == false)
    return;
    else
    // If the date, hour and minute of the alarm time are the same as
    // now, flash!
    {
    if (AlarmTime.Date == DateTime.Now.Date && AlarmTime.Hour ==
    DateTime.Now.Hour && AlarmTime.Minute == DateTime.Now.Minute)
    {
    // Makes lblAlarmVisible, and changes the backcolor based on
    // the value of blnColorTicker. The backcolor of the label
    // will flash once per tick of the clock.
    lblAlarm.Visible = true;
    if (blnColorTicker == false)
    {
    lblAlarm.BackColor = Color.Red;
    blnColorTicker = true;
    }
    else
    {
    lblAlarm.BackColor = Color.Blue;
    blnColorTicker = false;
    }
    }
    else
    {
    // Once the alarm has sounded for a minute, the label is made
    // invisible again.
    lblAlarm.Visible = false;
    }
    }
    }

The addition of this code accomplishes several tasks. The override statement directs the control to use this method in place of the method that was inherited from the base control. When this method is called, it calls the method it overrides by invoking the base.timer1_Tick statement, ensuring that all of the functionality incorporated in the original control is reproduced in this control. It then runs additional code to incorporate the alarm functionality. A flashing label control will appear when the alarm is triggered.

Your alarm clock control is almost complete. The only thing that remains is to implement a way to turn it off. To do this, we will add code to the lblAlarm_Click method.

To implement the shutoff method

  1. In Solution Explorer, right-click ctlAlarmClock.cs and click View Designer.

    The designer opens.

  2. Add a button to the control. Set the properties of the button as follows:

    Property............................Value
    Name..................................btnAlarmOff
    Text...................................Disable Alarm






  3. In the designer, double-click btnAlarmOff.

    The Code Editor opens to the private void btnAlarmOff_Click line.

  4. Modify this method so that it resembles the following:
    private void btnAlarmOff_Click(object sender, System.EventArgs e)
    {
    // Turns off the alarm
    AlarmSet = false;
    // Hides the flashing label
    lblAlarm.Visible = false;
    }
  5. From the File menu, choose Save All to save the project.

Testing Your Inherited Control

As with a standard user control, an inherited user control cannot stand alone and must be hosted in a form or other container. Since ctlAlarmClock has a greater depth of functionality, additional code is required to test it. In this section, you will write a simple program to test the functionality of ctlAlarmClock. You will write code to set and display the AlarmTime property of ctlAlarmClock, and will test its inherent functions.

To build and add your control to a test form

  1. In Solution Explorer, click ctlClockLib. On the Build menu, choose Build ctlClockLib.
  2. Add a new Windows Application project to the solution, and name it Test2.
  3. In Solution Explorer, right-click the References node for your test project. Click Add Reference to display the Add Reference window. Click the tab labeled Projects. Your project will be listed under Project Name. Double-click your project, and note that it now appears in the Selected Components window.
  4. In the Toolbox, click My User Controls.
  5. Scroll down until the icon for ctlAlarmClock comes into view.
  6. Double-click ctlAlarmClock to add a copy of ctlAlarmClock to your form.
  7. In the Toolbox, locate and double-click DateTimePicker to add a DateTimePicker control to your form, and add a Label control by double-clicking Label.
  8. Use the mouse to position the controls in a convenient place on the form.
  9. Set the properties of these controls as follows:

    Control..............Property...................Value
    label1.................Text..........................(leave blank)
    Name.......................lblTest
    datTimepicker Name.......................dtpTest
    Format....................Time
















  10. In the designer, double-click dtpTest.

    The Code Editor opens to private void dtpTest_ValueChanged.

  11. Modify the code so that it resembles the following:
    private void dtpTest_ValueChanged(object sender, System.EventArgs e)
    {
    ctlAlarmClock1.AlarmTime = dtpTest.Value;
    ctlAlarmClock1.AlarmSet = true;
    lblTest.Text = "Alarm Time is " +
    ctlAlarmClock1.AlarmTime.ToShortTimeString();
    }
  12. In Solution Explorer, right-click Test2 and choose Set as StartUp Project from the shortcut menu.
  13. From the Debug menu, choose Start.

    The test program starts. Note that the current time is updated in the ctlAlarmClock control, and that the starting time is shown in the DateTimePicker control.

  14. Click the DateTimePicker where the minutes of the hour are displayed.
  15. Using the keyboard, set a value for minutes that is one minute greater than the current time shown by ctlAlarmClock.

    The time for the alarm setting is shown in lblTest.

  16. Wait for the displayed time to reach the alarm setting time.

    When the displayed time reaches the time to which the alarm is set the lblAlarm will flash. Turn off the alarm by clicking btnAlarmOff. You may now reset the alarm.

This walkthrough has covered a number of key concepts. You have learned to create a user control by combining controls and components into a user control container. You have learned to add properties to your control, and to write code to implement custom functionality. In the second section, you learned to extend the functionality of a given user control through inheritence, and to alter the functionality of host methods by overriding those methods.

0 comments:

Post a Comment

Bejeweled

SNAKE Game - Top 20 Challenge

Learn c#.net

THE PRIMITIVE GAME - คนป่ามหาโหด