Windows Controls
All of the basic Windows controls work in much the same way as the TextBox and Button we have used so far.
Each of these controls has properties such as Name, Text, Font, Forecolor and Borderstyle that you can change most conveniently using the properties. You can also change these properties in your program code as well. One that method has been called, the rest of the controls have been created and you can change their properties in code. Generally, we will create a private init() method that is called right after the InitializeComponent method, in which we add any such additional initialization code.
Labels
A label is a field on the window form that simply displays text. Usually programmers use this to label the purpose of text boxes next to them. You can't click on a label or tab to it so it obtains the focus. However, if you want, you can change the major properties in Table 1 either in the
designer or at runtime.
Property |
Value |
Name |
At design time only |
BackColor |
A Color object |
BorderStyle |
None, FixedSingle or Fixed3D |
Enabled |
true or false. If false , grayed out. |
Font |
Set to a new Font object |
ForeColor |
A Color object |
Image |
An image to be displayed within the label |
ImageAlign |
Where in the label to place the image |
Text |
Text of the label |
Visible |
true or false |
Table 1 –Properties for the Label Control
TextBox
The TextBox is a single line or multiline editable control. You can set or get the contents of that box using its Text property:
TextBox tbox = new TextBox();
tbox.Text = "Hello there";
In addition to the properties in Table 1, the TextBox also supports the properties in Table 2.
Property |
Value |
Lines |
An array of strings, one per line |
Locked |
If true, you can't type into the text box |
Multiline |
true or false |
ReadOnly |
Same as locked. If true, you can still select the text and copy it, or set values
from within code. |
WordWrap |
true or false |
Table 2 – TextBox properties
CheckBox
A CheckBox can either be checked or not, depending on the value of the Checked property. You can set or interrogate this property in code as well as in the designer. You can create an event handler to catch the event when the box is checked or unchecked, by double clicking on the checkbox in the design mode.
CheckBoxes have a Appearance property which can be set to Appearance.Normal or Appearance.Button. When the appearance is set to the Button value, the control appears acts like a toggle button that stays depressed when you click on it and becomes raised when you click on it again. All the properties in Table 1 apply as well.
Buttons
A Button is usually used to send a command to a program. When you click on it, it causes an event that you usually catch with an event handler. Like the CheckBox, you create this event handler by double clicking on the button in the designer. All of the properties in Table 1 can be used as well.
Buttons are also frequently shown with images on them. You can set the button image in the designer or at run time. The images can be in bmp, gif, jpeg or icon files.
Radio buttons
Radio buttons or option buttons are round buttons that can be selected by clicking on them. Only one of a group of radio buttons can be selected at a time. If there is more than one group of radio buttons on a window form, you should put each set of buttons inside a Group box. As with checkboxes and buttons, you can attach events to clicking on these buttons by double clicking on them in the designer. Radio buttons do not always have events associated with them. Instead, programmers check the checked property of radio buttons when some other event, like an OK button click occurs.
Listboxes and Combo Boxes
Both list boxes and Combo boxes contain an Items array of the elements in that list. A ComboBox is a single line drop-down, that programmers use to save space when selections are changed less frequently. ListBoxes allow you to ser properties that allow multiple selections, but
ComboBoxes do not. Some of their properties include those in Table 3.
Property |
Value |
Items |
A collection of items in the list |
MultiColumn |
If true, the ColumnWidth property
describes the width of each column. |
SelectionMode |
One, MultiSimple or MultiExtended. If
set to MultiSimple, you can select or
deselect multiple items with a mouse
click. If set to MultiExtended, you can
select groups of adjacent items with a
mouse. |
SelectedIndex |
Index of selected item |
SelectedIndices |
Returns collection of selections when
list box selection mode is multiple. |
SelectedItem |
Returns the item selected |
Table 3 –The ListBox and ComboBox properties. SelectionMode and MultiColumn do not apply to combo boxes.
The Items Collection
You use the Items collection in the ListBox and ComboBox to add and remove elements in the displayed list. It is essentially an ArrayList. The basic methods are shown in Table 4.
Method |
Value |
Add |
Add object to list |
Count |
Number in list |
Item[i] |
Element in collection |
RemoveAt(i) |
Remove element i |
Table 4 – Methods for the Items Collection
If you set a ListBox to a multiple selection mode, you can obtain a collection of the selected items or the selected indexes by
ListBox.SelectedIndexCollection it =
new ListBox.SelectedIndexCollection(lsCommands);
ListBox.SelectedObjectCollection so =
new ListBox.SelectedObjectCollection(lsCommands);
where lsCommands is the list box name.
Menus
You add menus to a window by adding a MainMenu controls to the window form. Then, you can the menu control and edit its drop-down names and new main item entries. As with other clickable controls, double clicking on one in the designer creates an event whose code you can fill in.
ToolTips
A ToolTip is a box that appears when your mouse pointer hovers over a control in a window. This feature is activated by adding an (invisible) ToolTip control to the form, and then adding specific tool tips control and text combinations to the control. we add tooltips text to the button and list box using the tips control we have added to the window.
tips.SetToolTip (btPush, "Press to add text to list box");
tips.SetToolTip (lsCommands, "Click to copy to text box");
Shashi Ray