Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Controls

System.Windows.Forms Control Classes 

Class

Description

Button

Push buttons

CheckBox

Check boxes

CheckedListBox

List boxes whose items include check boxes

ComboBox

Combo boxes

DataGrid

Controls that display tabular data

DataGridTextBox

Edit controls hosted by DataGrid controls

DateTimePicker

Controls for selecting dates and times

GroupBox

Group boxes

HScrollBar

Horizontal scroll bars

Label

Label controls that display static text

LinkLabel

Label controls that display hyperlinks

ListBox

List boxes

ListView

List views (display flat lists of items in a variety of styles)

MonthCalendar

Month-calendar controls

NumericUpDown

Spinner buttons (up-down controls)

PictureBox

Controls that display images

PrintPreviewControl

Controls that display print previews

ProgressBar

Progress bars

PropertyGrid

Controls that list the properties of other objects

RadioButton

Radio buttons

RichTextBox

Rich-edit controls

StatusBar

Status bars

TabControl

Tab controls

TextBox

Edit controls

ToolBar

Toolbars

ToolTip

Tooltips

TrackBar

Track bars (slider controls)

TreeView

Tree views (display hierarchical lists of items)

VScrollBar

Vertical scroll bars

Creating a control and making it appear in a form is a three-step process:

  1. Instantiate the corresponding control class.

  2. Initialize the control by setting its property values.

  3. Add the control to the form by calling Add on the form’s Controls collection.

Button MyButton; // Field
.
.
.
MyButton = new Button ();
MyButton.Location = new Point (16, 16);
MyButton.Size = new Size (96, 24);
MyButton.Text = "Click Me";
Controls.Add (MyButton); 
MyButton.Click += new EventHandler (OnButtonClicked);
  .
  .
  .
void OnButtonClicked (Object sender, EventArgs e)
{
    // TODO: Respond to the button click
}

ControlDemo Example

using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

class MyForm : Form
{
    Label PathNameLabel; // four controls on this form
    TextBox PathNameBox;
    ListBox FileNameBox;
    Button ShowFileNamesButton;

    string CurrentPath;

    MyForm ()
    {
        // Set the form's title
        Text = "Control Demo";

        // Set the form's size
        ClientSize = new Size (256, 248);
        // Create the form's controls
        PathNameLabel = new Label ();
        PathNameLabel.Location = new Point (16, 16); 
        PathNameLabel.Size = new Size (224, 16);
        PathNameLabel.Text = "Path name";

        PathNameBox = new TextBox ();
        PathNameBox.Location = new Point (16, 32);
        PathNameBox.Size = new Size (224, 24);
        PathNameBox.TabIndex = 1;

        FileNameBox = new ListBox ();
        FileNameBox.Location = new Point (16, 72);
        FileNameBox.Size = new Size (224, 128);
        FileNameBox.TabIndex = 2;
        FileNameBox.DoubleClick += new EventHandler (OnShowFileInfo);

        ShowFileNamesButton = new Button ();
        ShowFileNamesButton.Location = new Point (112, 208);
        ShowFileNamesButton.Size = new Size (128, 24);
        ShowFileNamesButton.Text = "Show File Names";
        ShowFileNamesButton.TabIndex = 3;
        ShowFileNamesButton.Click +=
            new EventHandler (OnShowFileNames);

        // Add the controls to the form
        Controls.Add (PathNameLabel);
        Controls.Add (PathNameBox);
        Controls.Add (ShowFileNamesButton);
        Controls.Add (FileNameBox);
    }

    // Handler for the Show File Names button
    void OnShowFileNames (object sender, EventArgs e)
    {
        // Extract the path name from the TextBox control
        string path = PathNameBox.Text;

        if (path.Length > 0) {
            // Make sure the path name is valid
            if (Directory.Exists (path)) {
                // Empty the list box
                FileNameBox.Items.Clear ();

                // Get a list of all the files in the directory
                string[] files = Directory.GetFiles (path);

                // Put the file names (minus path names) in the
                // list box
                foreach (string file in files) {
                    FileAttributes attr = File.GetAttributes (file);
                    if ((attr & FileAttributes.Hidden) == 0)
                        FileNameBox.Items.Add (Path.GetFileName (file));
                }

                // Save the path name in case the user double-
                // clicks a file name
                CurrentPath = Path.GetFullPath (PathNameBox.Text);
            }
            // If the path isn't valid, notify the user
            else
                MessageBox.Show (path + " is not a valid path",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    // Handler for DoubleClick events from the ListBox
    void OnShowFileInfo (object sender, EventArgs e)
    {
        // Create a fully qualified file name from the file name
        // that the user double-clicked
        string file = CurrentPath;
        if (!file.EndsWith (":") && !file.EndsWith ("\\"))
            file += "\\";
        file += FileNameBox.SelectedItem.ToString ();

        // Display the dates and times that the file was created
        // and last modified
        DateTime created = File.GetCreationTime (file);
        DateTime modified = File.GetLastWriteTime (file);

        string msg = "Created: " + created.ToLongDateString () +
            " at " + created.ToLongTimeString () + "\n" +
            "Modified: " + modified.ToLongDateString () +
            " at " + modified.ToLongTimeString ();

        MessageBox.Show (msg, FileNameBox.SelectedItem.ToString (),
            MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    static void Main () 
    {
        Application.Run (new MyForm ());
    }
}
 

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.