[ Home | Syllabus | Course Notes | Assignments | Search]
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:
Instantiate the corresponding control class.
Initialize the control by setting its property values.
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 }
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 ()); } }
F1: Add to this Form's list of controls
F2: first parameter is control that posted this event, second is for additional informaiton (if needed)
F3: It would be nice to use visual studio to place these controls using a FORM editor.
F4: defines the "tabbing" order (using "Tab" key to move between controls