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

Lesson 13 - Basic Windows Forms Controls

In the previous lesson, DataGridView in C# .NET Windows Forms, we finished working with the DataGridView control. In today's C# .NET tutorial, we're going to begin a showcase of various controls in Windows Forms. There's a lot of them available in the Toolbox, and each has many properties and events. In several following lessons we'll describe the most commonly used controls and their most important properties and events.

Button

Button in Windows forms application - Form Applications in C# .NET Windows Forms

Let's start with the button. Button serves as a button you already know very well. Usually it's the key control in our apps that triggers an action based on additional information from the form.

Properties

Important properties include:

  • Text (string) - The text written on the button, i.e. its label. In the example above, the text is Button.
  • Image - The icon on the button.
  • ImageAlign - The alignment of the image to the button text.

Events

  • Click - Is invoked by clicking the button.

Label

Label in Windows forms application - Form Applications in C# .NET Windows Forms

Label is a text label that we put above or next to other controls. The user then knows how to use those controls. A Label with the "Name" text next to a text field tells the user that the program expects a name there.

Properties

  • Text (string) - The text of the label (in the example above it's Label).

Events

No important events.

TextBox

TextBox / Text field in Windows forms application - Form Applications in C# .NET Windows Forms

We get to the controls for entering values. TextBox is used to enter text. Although it could theoretically be used to enter values of various other types (like numbers, dates, ...), this is usually not appropriate use for this control, and there are another controls for such purposes. We can enter both short text as a single line (see the example above), or longer text as multiple lines. Such a the TextBox then looks like this:

Multiline TextBox in Windows forms application - Form Applications in C# .NET Windows Forms

Properties

  • Text (string) - The content of the text box (the entered text).
  • MultiLine (bool) - If set to true, we can resize the TextBox vertically as well and multiple text lines can be entered.
  • ScrollBars - If you decide to enter multiline text, it's recommended to set the scrollbars to be displayed in the case the text doesn't fit into the text box. We can choose between the vertical scrollbar (probably the most common), the horizontal one, both, and none.
  • Lines (String[]) - The content of the text box as a string array. We use this when the MultiLine property is set to true.
  • MaxLength (int) - Limits the text to the maximum number of characters.
  • ReadOnly (bool) - Allows us to set the text box as read-only. That is, to allow the user to only read and copy the content, but not to change it.
  • WordWrap (bool) - Enables word wrap.
  • PasswordChar (char) - If we want to enter a password into the text box, we can set the PasswordChar to some character (usually the asterisk *). Then all the characters of the text will be displayed as that character (asterisks), making the password entering more secure.

Events

  • TextChanged - Occurs when the text is changed, in other words, when any character is added into the text box. Be careful here how often the event occurs. When we type 100 characters, it'll be called 100 times (once for every character). It certainly shouldn't contain code that takes a long time to perform. A possible use of this event is, for instance, updating other text boxes and labels on the form when this text box is changed.

NumericUpDown

NumericUpDown / Numeric field in Windows forms application - Form Applications in C# .NET Windows Forms

NumericUpDown is used to enter numeric values. Although we could enter the number into a TextBox as well and then parse it to the int type, this isn't the smartest way to do that. The user can enter some nonsense in the box instead of the number. The NumericUpDown allows us to enter numbers only, so we can rely on the entered value, without needing to parse it and handling invalid inputs. In addition, we can also limit the number range.

Properties

  • Value (decimal) - The numeric value of the control. The value is of the decimal type. In our programs, we'll mostly need it as the int or float type, so we convert it using the Convert class, like this:
int i = Convert.ToInt32(NumericUpDown1.Value);
  • DecimalPlaces (int) - The number of decimal places. For integers we use 0.
  • Increment (decimal) - Specifies by what value the value of the control is incremented when clicking the up arrow (and decremented by the down arrow).
  • ReadOnly (bool) - The value of the control will be read-only.

Events

  • ValueChanged - Occurs when the value is changed. As with the TextBox, this event is triggered whenever a character is typed or when one of the arrows is clicked.

ComboBox

ComboBox / Selection field in Windows forms application - Form Applications in C# .NET Windows Forms

If we need to pick an item from several options, we can use 2 basic controls. The first one is ComboBox, which contains several items and allows us to select one of them. It's displayed as a drop-down menu thus saves space on the form.

Properties

  • Items (collection) - A non-generic collection of items. You can enter string items in it using the Visual Studio Designer. You can also insert any objects into the Items. They'll then be displayed as the returned values of their ToString() method. It's very useful to have whole objects added in these controls, as you'll see when we'll talk about the SelectedItem property.
  • SelectedItemIndex - The index of the selected item (the first item has the index of 0). Unfortunately, this property can't be set in the designer, so we often set in the form constructor to select the first item of the ComboBox at the application start up.
  • SelectedItem - The selected item. Unlike the previous property, it returns the selected item as an object. If we put objects in the Items (e.g. instances of a User class), we get the instance of the user selected in the ComboBox. We can then work with the instance very easily.
  • Sorted (bool) - If this property is set to true, the items will be kept sorted (e.g. alphabetically).
  • DataSource - Instead of adding items to the Items property, we can specify the DataSource. The control usually reacts to changes of the source, without us having to change the Items directly.
  • DisplayMember - In the data source, we usually have a collection of objects. This specifies the name of the object's property to be displayed in the list (e.g. Name if we have users in the DataSource and they have the Name property). So we don't have to overload the ToString() method, or we can display the objects in different ways.

Events

  • SelectedIndexChanged - Occurs when the selected item is changed.

ListBox

ListBox / Selection list in Windows forms application - Form Applications in C# .NET Windows Forms

ListBox is basically an expanded ComboBox that works almost the same. It allows us to pick an item from multiple options. Unlike the ComboBox, however, we can select multiple items (but don't have to).

Properties

In addition to the properties that are the same as of the ComboBox (i.e. Items, SelectedItemIndex, SelectedItem, and Sorted, see above), we can use few other:

  • SelectionMode - We can set multiple selection modes. One lets us select only one item at a time, MultiSimple lets us select multiple items at a time. MultiExtended lets us select more items using the Ctrl and Shift keys. None doesn't allow us to select any item at all.
  • SelectedItems - A collection of the selected items. We use this in case we've enabled multiple selection mode.
  • SelectedIndices - Returns a collection of indexes of the selected items.
  • MultiColumn (bool) - If set to true, the items will be displayed in multiple columns.

Events

  • SelectedIndexChanged - Occurs when a different item is selected.

CheckBox

CheckBox / Checkbox in Windows forms application - Form Applications in C# .NET Windows Forms

Simply put, CheckBox allows us to turn something on and off. It can be especially useful for settings of the application.

Properties

  • Checked (bool) - Indicates whether the CheckBox is checked or not.
  • Text (string) - The text description of the CheckBox (explains what we can turn on / off. Here it's set to CheckBox).

Events

  • CheckedChanged - Occurs when the check status of the check box is changed (that is, if we've checked or unchecked the box). Here we can respond to the change of settings and adjust the behavior of the application accordingly.

In the next lesson, Other Windows Forms Controls, we'll take a look at more advanced controls.


 

Previous article
DataGridView in C# .NET Windows Forms
All articles in this section
Form Applications in C# .NET Windows Forms
Skip article
(not recommended)
Other Windows Forms Controls
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