Sunday, December 19, 2010

[C#] - Numeric Updown and LinkLabel Control

1. Introduction

"Linklabel" is kind of label control but it serves the concept of a hyperlink. With this control, you can mark portion the label text or even the entire label text with an underline. Clicking on the underlined text raises the event "LinkClicked". By providing the handler for this event you can take an action.

"NumericUpDown Control" is a combination of the textbox and a pair of arrows pointing is opposite direction. Clicking the arrow or holding the mouse pointer on the arrow will increment or decrement the associated value. The current value is displayed on the text box part of the control. When we click the arrow an event "ValueChanged" is raised for taking the action.

2. NumericUpdown control

The "value property" of this control is used to retrieve the current numeric value of the control. This value property is changed when the up arrow or down arrow buttons are clicked.

Minimum and Maximum properties are used to set the limits of a numeric value. Say for example we want the value can be changed from 0 to 255 (For changing the colour value), then Minimum property is set to 0 and Maximum property is set to 255.

"Increment Property" is used to increment or decrement the values in steps. For example, if we set the Increment to 5, and if the current value of the control is 0, clicking twice the up arrow button will change the value property to 0 to 5 first and then 5 to 10 next.

The importance of "ValueChanged Event" is already discussed in the introduction.

3. LinkLabel Control

"LinkColor property" is used to set the colour of the Underlined portion of the label text. The underlined portion of the label text is called as a link. So "linkcolor" sets the colour for the link when it is not visited yet.

"LinkVisitedColor Property" is used to set the colour of the link when it is visited. When the "LinkVisited Property" is set to true, the control picks the colour from the LinkVisitedColor property and sets that colour to link.

The "LinkArea Property" is used to set portion of the label text as a link. This property determines that based on the letters involved in the label text and specifying the link starting location in terms of character and length of character from that location.

We can take the action for clicking a link portion of the label by providing a handler for the LinkClicked event.

4. Example – Take a Try

Launch “visual studio 2005” and Create Visual C# Windows application. Once the application is created design the form as shown below:

Use the table below to set the properties for the control shown in the above screenshot.  Each control number in the above screen shot is referred in the C.Number column.

5. Programming LinkLabel and NumericUpDown Control

I hope by looking at the dialog design you can understand how it will work and no more explanation is required.

1) Create an event handler for the ValueChanged event of the numericupdown control. In this handler, we get the changed value of the control to adjust the width of the text box. Below is the code for that:

//Updown_001: Increase width of the Text box based on the current value
//of the UpDown Control
private void nuUpDown_ValueChanged(object sender, EventArgs e)
    txtBx.Width = (int) nuUpDown.Value;               

Now, if you click the up or down arrow button the text box will be resized to its width.

2) Handle the linkclicked event for the control number 1.  In the handler, we first show a message box and then set the LinkVisited property to true. This will set the different colour for the link. Code is given below:

//LinkLabel_001: Handler for the Link clicked event. Display a message when
//the link clicked.
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    MessageBox.Show("Hello There..");
    linkLabel1.LinkVisited = true;

3) Handle the LinkClicked event for the control number 2. In the handler resize the height of the form to show more controls also set "LinkVisited Property" to true. Code is below:

//LinkLabel_002: Handler for the Link clicked event. Display the remaining
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    //001: Set that the link is visited
    linkLabel2.LinkVisited = true;

    //002: Resize the form to show all the controls
    this.Height = 200;

 Source Code : Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)