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
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 isButton
.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
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'sLabel
).
Events
No important events.
TextBox
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:
Properties
Text
(string
) - The content of the text box (the entered text).MultiLine
(bool
) - If set totrue
, we can resize theTextBox
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 astring
array. We use this when theMultiLine
property is set totrue
.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 thePasswordChar
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
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 thedecimal
type. In our programs, we'll mostly need it as theint
orfloat
type, so we convert it using theConvert
class, like this:
int i = Convert.ToInt32(NumericUpDown1.Value);
DecimalPlaces
(int
) - The number of decimal places. For integers we use0
.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 theTextBox
, this event is triggered whenever a character is typed or when one of the arrows is clicked.
ComboBox
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 theItems
. They'll then be displayed as the returned values of theirToString()
method. It's very useful to have whole objects added in these controls, as you'll see when we'll talk about theSelectedItem
property.SelectedItemIndex
- The index of the selected item (the first item has the index of0
). Unfortunately, this property can't be set in the designer, so we often set in the form constructor to select the first item of theComboBox
at the application start up.SelectedItem
- The selected item. Unlike the previous property, it returns the selected item as anobject
. If we put objects in theItems
(e.g. instances of aUser
class), we get the instance of the user selected in theComboBox
. We can then work with the instance very easily.Sorted
(bool
) - If this property is set totrue
, the items will be kept sorted (e.g. alphabetically).DataSource
- Instead of adding items to theItems
property, we can specify theDataSource
. The control usually reacts to changes of the source, without us having to change theItems
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 theDataSource
and they have theName
property). So we don't have to overload theToString()
method, or we can display the objects in different ways.
Events
SelectedIndexChanged
- Occurs when the selected item is changed.
ListBox
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 totrue
, the items will be displayed in multiple columns.
Events
SelectedIndexChanged
- Occurs when a different item is selected.
CheckBox
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 theCheckBox
is checked or not.Text
(string
) - The text description of theCheckBox
(explains what we can turn on / off. Here it's set toCheckBox
).
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.