Skip to content

NumericUpDown and LinkLabel Control in C#

1. Introduction to NumericUpDown and LinkLabel Control

LinkLabel Control is a kind of Label Control but it serves the thought of a hyper-link. With this LinkLabel Control, you can mark portion of the Label text or the full Label text with an underline. Clicking on the underlined text raises the LinkClicked Event. By adding the handler for this event, one can take a suitable action. NumericUpDown Control is a mix of the text box and a pair of arrows pointing in the opposite orders. Clicking the arrows of the NumericUpDown or holding down the mouse pointer on the arrow will increments or decrements the linked value. The linked value is shown on the text box part of the Control. When we click the arrow, C# raises ValueChanged  event allowing the C# developer to take the required action.

2. Properties of NumericUpDown Control

We use the Value Property of the NumericUpDown Control to fetch the present numeric value of the Control. C# changes this property value when a user clicks the up or down arrow buttons.

The Minimum and Maximum Property sets the limits for the numeric value of the NumericUpDown Control. Say, for example, if we want to change the value in the range between 0 and 255 (For RGB color value), then we set Minimum Property to 0 and Maximum Property to 255.

The Increment Property of the NumericUpDown Control rises or drops the value of the control in steps. For example, if we set the Increment Property 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 later 5 to 10.

We previously discussed the importance of ValueChanged event in the introduction section of this article.

3. Properties of LinkLabel Control

We use LinkColor Property of the LinkLabel Control sets the color of the underlined portion of the label text. The underscored portion of the label text is known as a link. So LinkColor sets the color for the link when it is not yet visited.

The LinkLabel’s LinkVisitedColor  Property sets the color of the already visited links. At run time, when we set the LinkVisted Property to true, C# will use the color in this property to change the color of the LinkLabel Control’s text.

We use the LinkArea Property of the LinkLabel to set specific part of the label text as a Hyper-Link. We can decide what part of the text we should mark as Hyper-Text by pointing out the starting point of the link in terms of character position and length of character from that starting point.

The Control raises the LinkClicked Event when user clicks the Link. A Developer can handle this event to take appropriate action.

4. Let us create our Example

To start the application, we have to launch Visual Studio 2005 and create Visual C# Windows Application. Once the IDE done with creating the application, we design the form as shown below:

NumericUpdown and LinkLabel Example
NumericUpdown and LinkLabel Example

One can use the table below to set the properties for the controls shown in the above screenshot. The first column in the below table refers the control number in the above screen.

NumericUpdown and LinkLabel Example - Control Properties
NumericUpdown and LinkLabel Example – Control Properties

5. Programming LinkLabel and NumericUpDown Control

5.1 ValueChanged Event Handler of NumericUpDown Control

Create an Event Handler for the ValueChanged Event of the NumericUpDown Control. In this C# Event Handler, we get the changed value of the NumericUpDown Control and use that value to adjust the width of the text box. Below is the code for that:

Now, if we click the up or down arrow button, the example will adjust the width of the text box. In the above code, the Value Property of the Control returns the current value of the Control. Also note, we type cast it as integer before setting the width of the TextBox Control.

5.2 LinkClicked Handler of LinkLabel Control

Next we 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 color for the link. Code is below:

Finally, we handle the LinkClicked Event for the control number 2. In the handler we resize the height of the form to show more controls also we set LinkVisited Property to true. Code is below:

6. Completed Example & Download

Below is the complete code example for the Form:

Source Code: Download from Google Drive

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: