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.
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.
Important properties include:
string) - The text written on the button, i.e. its label. In the example above, the text is
Image- The icon on the button.
ImageAlign- The alignment of the image to the button text.
Click- Is invoked by clicking the button.
Label is a text label that we put above or next to other
controls. The user then knows how to use those controls. A
"Name" text next to a text field tells the user that the
program expects a name there.
string) - The text of the label (in the example above it's
No important events.
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:
string) - The content of the text box (the entered text).
bool) - If set to
true, we can resize the
TextBoxvertically 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.
String) - The content of the text box as a
stringarray. We use this when the
MultiLineproperty is set to
int) - Limits the text to the maximum number of characters.
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.
bool) - Enables word wrap.
char) - If we want to enter a password into the text box, we can set the
PasswordCharto 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.
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 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.
decimal) - The numeric value of the control. The value is of the
decimaltype. In our programs, we'll mostly need it as the
floattype, so we convert it using the
Convertclass, like this:
int i = Convert.ToInt32(NumericUpDown1.Value);
int) - The number of decimal places. For integers we use
decimal) - Specifies by what value the value of the control is incremented when clicking the up arrow (and decremented by the down arrow).
bool) - The value of the control will be read-only.
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.
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
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
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
ComboBoxat 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
Userclass), we get the instance of the user selected in the
ComboBox. We can then work with the instance very easily.
bool) - If this property is set to
true, the items will be kept sorted (e.g. alphabetically).
DataSource- Instead of adding items to the
Itemsproperty, we can specify the
DataSource. The control usually reacts to changes of the source, without us having to change the
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.
Nameif we have users in the
DataSourceand they have the
Nameproperty). So we don't have to overload the
ToString()method, or we can display the objects in different ways.
SelectedIndexChanged- Occurs when the selected item is changed.
ListBox is basically an expanded
works almost the same. It allows us to pick an item from multiple options.
ComboBox, however, we can select multiple items (but
don't have to).
In addition to the properties that are the same as of the
Sorted, see above), we can use few
SelectionMode- We can set multiple selection modes.
Onelets us select only one item at a time,
MultiSimplelets us select multiple items at a time.
MultiExtendedlets us select more items using the Ctrl and Shift keys.
Nonedoesn'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.
bool) - If set to
true, the items will be displayed in multiple columns.
SelectedIndexChanged- Occurs when a different item is selected.
CheckBox allows us to turn something on and off. It
can be especially useful for settings of the application.
bool) - Indicates whether the
CheckBoxis checked or not.
string) - The text description of the
CheckBox(explains what we can turn on / off. Here it's set to
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.
No one has commented yet - be the first!