0% found this document useful (0 votes)
2 views

COMPROG1

The document provides an overview of various C# form controls, including their properties, methods, and typical usage. It covers controls such as MessageBox, Button, Label, TextBox, GroupBox, Panel, RadioButton, and ListBox, detailing how to manipulate them and their specific functionalities. Additionally, it explains how to group controls and manage user interactions through events.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

COMPROG1

The document provides an overview of various C# form controls, including their properties, methods, and typical usage. It covers controls such as MessageBox, Button, Label, TextBox, GroupBox, Panel, RadioButton, and ListBox, detailing how to manipulate them and their specific functionalities. Additionally, it explains how to group controls and manage user interactions through events.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C# Form Controls

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

There are many more types of controls than these few.

MessageBox

A MessageBox normally gives us a small window with a message to the user. It is a


"modal" window, blocking other actions until it is closed.

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.

The principal events for a Button are Click and DoubleClick.

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.

A Label has various properties (color, font, 3-D appearance, etc.).

TextBox

A TextBox is used either to display text or to accept text as input. By default, this is
one line.

The TextBox class has many properties:

 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.

As a control, a TextBox has events, such as "TextChanged".


GroupBox

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

A Panel is a container for other controls. In some ways it is similar to a GroupBox.


There are some differences:

 A Panel can have scroll bars, but a GroupBox cannot.


 A Panel does not have a caption, but a GroupBox can have one.
 A Panel is by default displayed without a border, but a GroupBox by default
does display a border.

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;

or this can be done in the Designer using the "properties" menu.


How do we know which RadioButton was selected? We use the CheckedChanged
event, as in
this.radioButton1.CheckedChanged += new
EventHandler(radioButton_CheckedChanged);

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.

An alternative to enlcosing a group of RadioButtons in a GroupBox or a Panel is to


use the GroupName property (a string). If two or more RadioButtons have the same
value (not null or empty), they should be mutually exclusive.

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.

You might also like