COMPROG1
COMPROG1
There is an actual Control class which is the base class for all of the controls we find
in the ToolBox menu. We do not normally use the Control class itself.
It is worth noticing that not all controls have the same features. For instance, not all of
them have scroll bars.
We typically add a control to a form by selecting it from the ToolBox menu and
dragging it into the form's window.
Any control will have properties. We can set some of them by right-clicking on the
control to get the "properties" menu. Properties include the location of the control, the
color, its caption, text entered by the user, etc. Likewise, we can reposition the control
inside the form with the mouse, and this also changes some of the properties
(location).
We can use the Show() and Hide methods to make the control visible or not visible,
respectively.
Some controls may act as containers for other controls. For instance, if we have radio
buttons, we will normally put these in a groupbox or a panel.
A control will have a name, such as "button1". We can change the name of the
control, but it must be changed everywhere it appears. This may be a lot of work. If
MessageBox
To display a MessageBox, call the static method MessageBox.Show. This method can
have numerous parameters, of which the first is always a string to be displayed.
Depending on the parameters, it is quite possible for a MessageBox to contain other
items (buttons, icons, etc.), but often all we have (and all we want) is to display a
message and an "OK" button on it. which the user clicks to closed the MessageBox.
Button
A Button control is what it sounds like: we can click on it or press it. It should have a
bit of text on it: the Text property.
This could be used in various ways: indicate yes or no, give the user a piece of
information before continuing, etc.
Label
A Label control is typically used to provide a text description for another control such
as a TextBox or to provide instructions at the top of a form.
Nonetheless, a Label is a control, and thus has many options inherited from Control. It
will have an event handler, which may often be left empty if all we want is some text.
TextBox
A TextBox is used either to display text or to accept text as input. By default, this is
one line.
The Text property allows us to get or set the text in the control.
Set the Multiline property to true and use the ScrollBars property to enable the
use of multiple lines of text.
The WordWrap property indicates whether words should automatically wrap to
the beginning of the next line when necessary.
Whether scroll bars will be visible can be indicated by setting ScrollBars =
"Both", "Horizontal", "None" or "Vertical". (This is actually an enumeration.)
Horizontal scroll bars do not appear if the WordWrap property is set to true.
If a TextBox control is to be used only for displaying text, set the ReadOnly
property to true.
A GroupBox is a container (marked with a frame) for a group of other controls. This
is used to group controls together logically. A GroupBox can have a caption (the Text
property).
The typical use is to contain a set of RadioButton controls. We could have several
Groupbox controls, each containing several RadioButtons, and the user would be able
to select one choice from each set.
If a GroupBox is disabled (setting its Enabled property to false), all the controls
contained in it are also disabled. The same is true for the Visible property.
Panel
RadioButton
The idea of a RadioButton is that we have a set of choices and we want the user to
select exactly one of them. We have a RadioButton for each choice, and we enclose
the set of RadioButtons in a GroupBox or a Panel.
We can preset one of the RadioButton controls as the default using the "checked"
property:
radioButton1.Checked = true;
We do this for all the RadioButton controls in our set, and later, in the event handler,
we can tell them apart as one of them will be the sender of the event:
void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
RadioButton selectedrb;
if (rb == null)
{
MessageBox.Show("Sender is not a RadioButton");
return;
}
// Ensure that the RadioButton.Checked property
// changed to true.
if (rb.Checked)
{
// Keep track of the selected RadioButton by saving a reference
// to it.
selectedrb = rb;
}
}
and we could then (still in the handler) use the value of selectedrb to figure out which
choice was made.
ListBox
A ListBox control can be used to present a list of items to the user and to let the user
then select one or more of them. Here an item is of type object, so it might be almost
anything. (For now, think of strings.)
One of the properties of a ListBox is Items, which represents the collection of items in
the ListBox control and is an instance of the ListBox.ObjectCollection class. This
latter class has a method called Add() we can use to add items to the ListBox control
and a method called remove() to remove items. For example:
ListBox listBox1 = new ListBox();
listBox1.Size = new System.Drawing.Size(200, 100); // size of ListBox
listBox1.Location = new System.Drawing.Point(10,10); // location of
ListBox
this.Controls.Add(listBox1); // add to the form
for (int x = 1; x <= 50; x++) // add items
{
listBox1.Items.Add("Item " + x.ToString());
}
If we set the MultiColumn property to true, the items will be displayed in columns, as
many as are needed to make vertical scrolling unnecessary. If the HorizontalScrollbar
property is set to true, the user can then scroll to see columns not previously shown.
The ColumnWidth property indicates the width (in pixels) of each column; the default
is 0, which provides a (nonzero) default value.