FREE BOOK

Writing Windows C# Programs

Posted by Addison Wesley Free Book | C# Language July 21, 2009
The C# language has its roots in C++, Visual Basic, and Java. Both C# and VB.Net use the same libraries and compile to the same underlying code. Both are managed languages with garbage collection of unused variable space, and both can be used interchangeably. Both also use classes with method names that are very similar to those in Java, so if you are familiar with Java, you will have no trouble with C#.

Windows Controls

All of the basic Windows controls work in much the same way as the TextBox and Button we have used so far. Many of the more common ones are shown in the Windows Controls program in Figure 3-4.
 
 
 
 Figure 3-4 A selection of basic Windows controls
 
Each of these controls has properties such as Name, Text, Font, Forecolor, and Borderstyle that you can change most conveniently using the properties window shown at the right of Figure 3-2. You can also change these properties in your program code as well. The Windows Form class that the designer generates always creates a Form1 constructor that calls an InitializeComponent method like the preceding one. Once 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 Initialize-Component 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 3-1 either in the designer or at runtime.

Table 3-1
Properties for the Label Control

  

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

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 3-1, the TextBox also supports the properties in Table 3-2.

CheckBox

A CheckBox can be either 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 an Appearance property that can be set to Appearance. Normal or Appearance.Button. When the appearance is set to the Button value,

Table 3-2 TextBox Properties

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

the control 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 3-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 3-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 runtime. 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 we did in the program in Figure 3-4. 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 ComboBoxes

Both ListBoxes and ComboBoxes 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 set properties that allow multiple selections, but ComboBoxes do not. Some of their properties include those in Table 3-3.

Table 3-3 The ListBox and ComboBox Properties

Property Value
Items A collection of items in the list
MultiColumn If true, the ColumnWidth property
describes the width of each column.
(Does not apply to ComboBox.)
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.
(Does not apply to ComboBox.)
SelectedIndex Index of selected item
SelectedIndices Returns collection of selections when
ListBox selection mode is multiple.
SelectedItem Returns the item selected

Total Pages : 6 23456

comments