Get up to 80 % extra points for free! More info:

Lesson 16 - Windows Forms Controls For The Fourth Time

In the previous lesson, Windows Forms - Dialogs, we took a look at application menus and dialogs. Today, we're going to look at how to use RichTextBox, MaskedTextBox, TabControl, LinkLabel, Timer and ProgressBar.

MaskedTextBox

MaskedTextBox in Windows Forms - Form Applications in C# .NET Windows Forms

This is a special type of TextBox which allows us to specify in what format the user will be able to enter data into it. This is useful, for example, for entering telephone numbers and other similar data. The user can't enter anything other than the allowed format into the box, therefore there's no need to validate the value.

The key property here is Mask, which specifies the format of the entered value. If we don't choose from the predefined ones, we can easily create our own. We'll select <Custom> and enter our desired format into the Mask box below. For example, 00/00/0000 would be a format for entering a specific date.

In addition to the zeros, we can use the L symbol for letters. If we want to allow any character we use ?. A allows the user to enter either a letter or a number. 0 is only for numbers, and 9 allows numbers + free space.

RichTextBox

RichTextBox in Windows Forms - Form Applications in C# .NET Windows Forms

RichTextBox can be described as "a TextBox on steroids". It allows advanced text formatting and other tweaks. The main thing is that we can change the font or color for specific part of text only. Of course, we can do that for the entire text using the ForeColor (text color) and the BackColor (background color) properties, but this can be done even with the TextBox.

We select a specific part of the text by using the Select() method. It takes the indexes of the selection we want to format as parameters - the start index and end index. We can simply pass numbers or properties/methods, as is illustrated by the example below.

richTextBox1.Select(richTextBox1.Text.IndexOf("text"), "text".Length);
// or
richTextBox1.Select(0, 6);

The code selects the text "text" and then allows us to work with it. Of course, we could've used LastIndexOf() as well. The selected text is then accessed using Selection[Font, BackColor, etc.]. So styling part of the text would look something like this:

richTextBox1.SelectionFont = new Font("Microsoft sans", 20);
richTextBox1.SelectionBackColor = Color.Wheat;
richTextBox1.SelectionColor = Color.Navy;

Of all the properties, it's worth mentioning MaxLength, which specifies the maximum number of characters that the RichTextBox can contain. RightMargin specifies the margin on the right side. ZoomFactor is also quite useful and serves for zooming the contents.

There are many events. TextChanged will come in handy, and as the name already suggests, it's called when the text has changed. LinkClicked is triggered when a URL link in the text has been clicked.

LinkLabel

LinkLabel in Windows Forms - Form Applications in C# .NET Windows Forms

As can be deduced from the name (as with the other controls), it's simply a Label that acts as a link to a website. The properties are also similar to the ordinary Label. However, its use isn't very intuitive. By using the LinkArea property, we specify which part of the LinkLabel can be clicked on. Start specifies the start index, and Length specifies the number of characters. The basic usage could look like this:

LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = "https://www.ict.social/";
linkLabel1.Links.Add(link);

We put the code above into the form constructor or the Load event handler of the form. The main event of LinkLabel is LinkClicked. It's necessary to write the following code in it:

Process.Start(e.Link.LinkData as String);

It'll send a command to the system, and the set URL will be opened in the default browser. To use this, you must first add using System.Diagnostics.

Timer

This is a very handy control that allows us to perform certain actions automatically after a given time interval has elapsed. The key property here is Interval, which specifies the interval between actions being performed in milliseconds. 60.000 ms is 1 minute. Quite important is also the Enabled property, which specifies whether the Timer is active when the app is launched. The second option is to start it using the Start() method. The Stop() method then logically stops the Timer.

Timer doesn't have its own thread, so keep in mind that when you perform more complex actions, it may freeze the entire form.

Timer has only a single event, which is Tick being invoked in given time intervals.

TabControl

TabControl in Windows Forms - Form Applications in C# .NET Windows Forms

This is a control with tabs, which is best explained by the picture above. It contains several tabs in which we can place other controls (similarly as with Panel) and then switch between the tabs. We can add individual "tabs" in the TabControl properties under the TabPages item, where we can also name them. The tabs act as containers, in which we can add additional controls. Other properties only modify the appearance.

You can also use the TabControl as an installation wizard. The tab bar can't be hidden, but you can hide it outside of the window, as shows the screenshot below.

TabControl with hidden bookmarks in Windows Forms - Form Applications in C# .NET Windows Forms

We can then switch to the next tab in the code, e.g. by using its name as follows:

tabControl1.SelectTab("[name of the tab]");

Or by its index:

tabControl1.SelectedIndex = [index];

ProgressBar

Progressbar in Windows Forms - Form Applications in C# .NET Windows Forms

It's useful, for example, to inform the user of the progress of some process in the application. The Maximum property specifies how many "fields" the progressbar has. The Style property is also important, which sets the visual style of the progress, and finally Step, which specifies how much will be filled when we call the PerformStep() method. There are three options to choose from - Blocks, Continuous, and Marquee. The last one is different. If set, the ProgressBar doesn't show a progress, but "runs" endlessly instead. You can also set the speed, which can be found as the MarqueeAnimationSpeed property.

The Marquee ProgressBar can't be simply turned on, so we need to use a little "trick". If we want it to run not before we activate it, we must first set it to Continuous and then set somewhere in the code:

progressBar1.Style = ProgressBarStyle.Marquee;

We can stop it in the same way.

progressBar1.Style = ProgressBarStyle.Continuous;

To reset the ProgressBar, we set its Value to 0. We can also hide it using the Hide() method and then make it appear again using the Show() method. In the next lesson, Custom Controls In C# .NET, we'll show how to create our own controls.


 

Previous article
Windows Forms - Dialogs
All articles in this section
Form Applications in C# .NET Windows Forms
Skip article
(not recommended)
Custom Controls In C# .NET
Article has been written for you by David Capka Hartinger
Avatar
User rating:
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities