Using Windows Forms Controls


Controls are definitely the key ingredient for development of Windows applications. All .NET Framework Windows Forms controls are subclasses of the System.Windows.Forms.Control class, which itself inherits System.ComponentModel.Component class. Even the Form class is really a special type of control (a Container control to be specific, Form inherits ContainerControl). All the controls that are defined by the .NET Framework are included in the System.Windows.Forms namespace. Using controls typically involves instantiating them, setting a set of properties, and adding them to a parent (typically a form or a container control such as Panel/ Group Box). For instance, by reexamining the Hello World GUI application, you will notice that it consists of a single control, ExitButton, which is of the type System.Windows.Forms.Button.

 
 ...    private Button ExitButton; ...    ExitButton = new Button();    ExitButton.Text = "Exit!";    Controls.Add(ExitButton); ... 

Event Handlers

Apart from the basic control creation operations (creating, setting properties, and adding them to containers and forms), controls also enable you to attach event handlers. Event handlers perform the related set of actions that your GUI interaction logic requires. For instance, the Hello World application contains a single event handler, ExitButton_Click, attached to the ExitButton Control.

 
 using System.Windows.Forms;       ...  ExitButton.Click += new EventHandler(this.ExitButton_Click);  ...    }    protected void ExitButton_Click(Object sender, EventArgs e)    {       Application.Exit();    }    ...    }  } } 

SHOP TALK : APPLYING THE MODEL VIEW CONTROLLER (MVC) DESIGN PATTERN IN GUI APPLICATIONS

Whenever GUI applications are mentioned, so is the classical Model View Controller (MVC) design pattern. MVC basically means that as an application designer, you divide your application into a set of independent components that can be loosely connected. Consider an order-entry application. Here, the model is a business object called Order, which provides the capability to search for existing orders, create new orders, or update existing orders. The View is an Order form which can be prepopulated with an existing order or can be used to enter a new order. The controller is an event handler that is registered with specific user actions (such as pressing a button or selecting a menu item) and that invokes the appropriate model. This way, if your back-end application logic changes tomorrow (suppose you move to a new order processing application), or you intend to change the view (for instance, utilizing the Outlook style folders), you can easily change the individual pieces of the code without touching the other components. Windows forms facilitate this in a number of ways by using a control, event handler paradigm. In my opinion, it is your job to take this further and establish a set of business model components that are kept independent of their usage ”for instance, modeled as .NET Serviced Components.


Laying Out Controls

Because controls are added to a form (or another control container), you are required to lay them out. Controls can be positioned explicitly on the form canvas by setting the Location and Size properties.

 
 sendButton.Location = new Point(136, 72); sendButton.Size = new Size(72, 24); sendButton.Text = "Send"; 

Apart from the explicit positioning of controls, the Windows Forms application model also allows the user to set up additional properties for anchoring and docking controls. These additional properties allow developers to create resizable forms (resizing occurs when the user maximizes or resizes a particular form). Two basic layout mechanisms are available for the developer with an advanced custom layout capability available for any custom requirements.

Docking Controls

Docking allows controls to be " docked " to one of the five positions of a screen (top, left, right, bottom, and fill). The position is said to be the "hug" for a control. For instance, if a Status Bar control is docked to the bottom edge, it will fill the bottom edge of the screen. The simple docking illustration application (Listing 7.1, shown in Figure 7.3) shows the five buttons docked with the five possible positions. Notice the behavior of the form after it is resized as shown in Figure 7.4 (shrink or expand).

Listing 7.1 Docking Controls
 using System; using System.Windows.Forms; namespace MyCompany {  namespace MyApp  {    class DockingForm : Form {    public DockingForm()    {       this.Text = "Docking Windows Forms Application";       Button topButton = new Button();       topButton.Text = "Top";       Button leftButton = new Button();       leftButton.Text = "Left";       Button rightButton = new Button();       rightButton.Text = "Right";       Button bottomButton = new Button();       bottomButton.Text = "Bottom";       Button fillButton = new Button();       fillButton.Text = "Fill";       Controls.Add(topButton);       Controls.Add(leftButton);       Controls.Add(rightButton);       Controls.Add(bottomButton);       Controls.Add(fillButton);       topButton.Dock = DockStyle.Top;       leftButton.Dock = DockStyle.Left;       rightButton.Dock = DockStyle.Right;       bottomButton.Dock = DockStyle.Bottom;       fillButton.Dock = DockStyle.Fill;    }    public static void Main()    {       DockingForm df = new DockingForm();       Application.Run(df);    }    }  } } 
Figure 7.3. Docking form controls.

Figure 7.4. Resizing a docked form.

Anchoring Controls

Controls can also be anchored to a part of the screen (top, left, right, bottom, or a combination of these). Anchoring ensures that the controls will be expanded (or reduced) to occupy the screen area. For instance, the instant messaging application that follows (Listing 7.2, shown in Figure 7.5) allows the message text box to be expanded with the screen resizing (Figure 7.6) (using a Top+Left+Right anchor) but keeps the two buttons (Send and Clear) anchored to the bottom-right corner of the screen.

Listing 7.2 Anchoring Controls
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace AnchorApp {    public class Form1 : Form    {       private TextBox messageBox;       private Label label1;       private Button sendButton;       private Button clearButton;       private System.ComponentModel.Container components = null;       public Form1()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          if( disposing )          {             if (components != null)             {                components.Dispose();             }          }          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.messageBox = new TextBox();          this.label1 = new Label();          this.sendButton = new Button();          this.clearButton = new Button();          this.SuspendLayout();          //          // messageBox          //          this.messageBox.Anchor = ((AnchorStyles)                             (((AnchorStyles.Top  AnchorStyles.Left)              AnchorStyles.Right)));          this.messageBox.Font = new Font("Microsoft Sans Serif", 8.25F,                             FontStyle.Bold,                             GraphicsUnit.Point, ((Byte)(0)));          this.messageBox.Location = new Point(8, 32);          this.messageBox.Name = "messageBox";          this.messageBox.Size = new Size(280, 20);          this.messageBox.TabIndex = 0;          this.messageBox.Text = "Type your Message here ...";          //          // label1          //          this.label1.Font = new Font("Microsoft Sans Serif", 8.25F,                            FontStyle.Bold, GraphicsUnit.Point, ((System.Byte)(0)));          this.label1.Location = new Point(8, 8);          this.label1.Name = "label1";          this.label1.Size = new Size(56, 16);          this.label1.TabIndex = 1;          this.label1.Text = "Message";          //          // sendButton          //          this.sendButton.Anchor = ((AnchorStyles)                             ((AnchorStyles.Bottom  AnchorStyles.Right)));          this.sendButton.Location = new Point(136, 72);          this.sendButton.Name = "sendButton";          this.sendButton.Size = new Size(72, 24);          this.sendButton.TabIndex = 2;          this.sendButton.Text = "Send";          //          // clearButton          //          this.clearButton.Anchor = ((AnchorStyles)                            ((AnchorStyles.Bottom  AnchorStyles.Right)));          this.clearButton.Location = new Point(216, 72);          this.clearButton.Name = "clearButton";          this.clearButton.Size = new Size(72, 24);          this.clearButton.TabIndex = 3;          this.clearButton.Text = "Clear";          //          // Form1          //          this.AutoScaleBaseSize = new Size(5, 13);          this.ClientSize = new Size(292, 101);          this.Controls.Add(this.clearButton);          this.Controls.Add(this.sendButton);          this.Controls.Add(this.label1);          this.Controls.Add(this.messageBox);          this.Name = "Form1";          this.Text = "Anchoring Windows Forms Controls";          this.ResumeLayout(false);       }       [STAThread]       static void Main()       {          Application.Run(new Form1());       }    } } 
Figure 7.5. Anchoring form controls.

Figure 7.6. Resizing anchored forms.

Windows Forms Controls Quick Reference

Apart from the flexibility and consistency of the developer's choice of programming language, the .NET Framework also provides a rich set of out-of-the-box controls that can be used to build rich and interactive forms applications. Table 7.1 lists the set of basic controls used for user input, display, and grouping.

TOWARD A LAYOUT MANAGER: DOCKING AND ANCHORING

If you are familiar with Java, you are probably aware of the concept of the layout manager. In particular, the .NET Framework itself lacks something like a GridBagLayoutManager. Whereas docking and anchoring enable developers to develop resizable applications, they still lack the tremendous flexibility of a layout manager. This is one capability that is expected to be included in future versions of the .NET Framework.


Table 7.1. Windows Forms Controls (Not an Exhaustive List)

WINDOWS FORMS CONTROL

DESCRIPTION

BASIC CONTROLS

TextBox

Input text (single line/multiline)

RichTextBox

Input text with formatting, Rich Text Format (RTF) content

Label

Display static text

LinkLabel

Displays text linked to a Web resource

StatusBar

Application status bar

CheckBox

Check box with label; used to get settings for multiple possible values

RadioButton

On/off radio button (yes/no input value)

TrackBar

Scale-based input

CheckedListBox

Scrollable list of items with a check box

ComboBox

Show a drop down list of items

DomainUpDown

Scrollable list of items using up/down buttons

ListBox

List of text and icon items

ListView

List of items in different views (text only, with small icons, with large icons, details)

NumericUpDown

List of scrollable numbers using up/down buttons

PictureBox

Display a bitmap

ImageList

Image repository

Panel

Scrollable but unlabeled container

GroupBox

Labeled unscrollable container

ADVANCED CONTROLS

TabControl

Tabbed panel layout

TreeView

Tree-based hierarchical display of a list of items

DateTimePicker

Calendar-based date/time entry

MonthCalendar

Calendar-based date range entry

ColorDialog

Select a color dialog

FontDialog

Select a font dialog

OpenFileDialog

Open a file dialog

PrintDialog

Select a printer dialog

PrintPreviewDialog

Print preview

SaveFileDialog

Save a file dialog

Toolbar

Application toolbar ”set of buttons

Timer

Raise an event at regular intervals

ToolTip

Context-sensitive help for controls

DataGrid

Tabular data grid control for viewing/updating datasets

Splitter

Split a docked control

ProgressBar

Graphical progress indicator

HelpProvider

Associate a Windows HTML Help file with a Windows application

Listing 7.3 shows the basic form controls ”TextBox, Label, ComboBox, GroupBox ”utilized to develop a customer entry form (Figure 7.7).

Listing 7.3 Using Windows Forms Controls
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CustomerEntryApplication {    public class CustomerEntryForm : Form    {       private Label formLabel;       private TextBox zipCodeBox;       private ComboBox stateBox;       private Label stateLabel;       private TextBox cityBox;       private Label cityLabel;       private TextBox streetTextBox;       private Label streetLabel;       private Label marriedLabel;       private Label ageGroupLabel;       private TextBox lastNameTextBox;       private Label lastNameLabel;       private TextBox miTextBox;       private Label middleInitialLabel;       private TextBox firstNameTextBox;       private Label firstNameLabel;       private GroupBox basicDetailGroup;       private GroupBox addressGroup;       private GroupBox otherInfoGroup;       private RadioButton NoRadioButton;       private RadioButton YesRadioButton;       private CheckBox Over35Box;       private CheckBox B1835Box;       private CheckBox Under18Box;       private System.ComponentModel.Container components = null;       public CustomerEntryForm()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          if( disposing )          {             if (components != null)             {                components.Dispose();             }          }          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.formLabel = new Label();          this.addressGroup = new GroupBox();          this.zipCodeBox = new TextBox();          this.stateBox = new ComboBox();          this.stateLabel = new Label();          this.cityBox = new TextBox();          this.cityLabel = new Label();          this.streetTextBox = new TextBox();          this.streetLabel = new Label();          this.otherInfoGroup = new GroupBox();          this.NoRadioButton = new RadioButton();          this.YesRadioButton = new RadioButton();          this.Over35Box = new CheckBox();          this.B1835Box = new CheckBox();          this.Under18Box = new CheckBox();          this.marriedLabel = new Label();          this.ageGroupLabel = new Label();          this.basicDetailGroup = new GroupBox();          this.lastNameTextBox = new TextBox();          this.lastNameLabel = new Label();          this.miTextBox = new TextBox();          this.middleInitialLabel = new Label();          this.firstNameTextBox = new TextBox();          this.firstNameLabel = new Label();          this.addressGroup.SuspendLayout();          this.otherInfoGroup.SuspendLayout();          this.basicDetailGroup.SuspendLayout();          this.SuspendLayout();          //          // formLabel          //          this.formLabel.Font = new Font("Microsoft Sans Serif", 8.25F,                            FontStyle.Bold, GraphicsUnit.Point, ((Byte)(0)));          this.formLabel.Location = new Point(0, 0);          this.formLabel.Name = "formLabel";          this.formLabel.Size = new Size(312, 16);          this.formLabel.TabIndex = 0;          this.formLabel.Text = "Customer Entry Form";          this.formLabel.TextAlign = ContentAlignment.TopCenter;          //          // addressGroup          //          this.addressGroup.Controls.Add(this.zipCodeBox);          this.addressGroup.Controls.Add(this.stateBox);          this.addressGroup.Controls.Add(this.stateLabel);          this.addressGroup.Controls.Add(this.cityBox);          this.addressGroup.Controls.Add(this.cityLabel);          this.addressGroup.Controls.Add(this.streetTextBox);          this.addressGroup.Controls.Add(this.streetLabel);          this.addressGroup.Location = new Point(4, 112);          this.addressGroup.Name = "addressGroup";          this.addressGroup.Size = new Size(304, 88);          this.addressGroup.TabIndex = 2;          this.addressGroup.TabStop = false;          this.addressGroup.Text = "Address Information";          //          // zipCodeBox          //          this.zipCodeBox.Location = new Point(216, 64);          this.zipCodeBox.MaxLength = 5;          this.zipCodeBox.Name = "zipCodeBox";          this.zipCodeBox.Size = new Size(72, 20);          this.zipCodeBox.TabIndex = 7;          this.zipCodeBox.Text = "";          //          // stateBox          //          this.stateBox.Items.AddRange(new object[] {"PA","NJ","NY"});          this.stateBox.Location = new Point(120, 64);          this.stateBox.Name = "stateBox";          this.stateBox.Size = new Size(40, 21);          this.stateBox.TabIndex = 6;          //          // stateLabel          //          this.stateLabel.Location = new Point(8, 64);          this.stateLabel.Name = "stateLabel";          this.stateLabel.Size = new Size(104, 16);          this.stateLabel.TabIndex = 4;          this.stateLabel.Text = "State";          //          // cityBox          //          this.cityBox.Location = new Point(120, 38);          this.cityBox.MaxLength = 100;          this.cityBox.Name = "cityBox";          this.cityBox.Size = new Size(72, 20);          this.cityBox.TabIndex = 5;          this.cityBox.Text = "";          //          // cityLabel          //          this.cityLabel.Location = new Point(8, 38);          this.cityLabel.Name = "cityLabel";          this.cityLabel.Size = new Size(104, 16);          this.cityLabel.TabIndex = 2;          this.cityLabel.Text = "City";          //          // streetTextBox          //          this.streetTextBox.Location = new Point(120, 16);          this.streetTextBox.Name = "streetTextBox";          this.streetTextBox.Size = new Size(176, 20);          this.streetTextBox.TabIndex = 4;          this.streetTextBox.Text = "";          //          // streetLabel          //          this.streetLabel.Location = new Point(8, 16);          this.streetLabel.Name = "streetLabel";          this.streetLabel.Size = new Size(104, 16);          this.streetLabel.TabIndex = 0;          this.streetLabel.Text = "Street";          //          // otherInfoGroup          //          this.otherInfoGroup.Controls.Add(this.NoRadioButton);          this.otherInfoGroup.Controls.Add(this.YesRadioButton);          this.otherInfoGroup.Controls.Add(this.Over35Box);          this.otherInfoGroup.Controls.Add(this.B1835Box);          this.otherInfoGroup.Controls.Add(this.Under18Box);          this.otherInfoGroup.Controls.Add(this.marriedLabel);          this.otherInfoGroup.Controls.Add(this.ageGroupLabel);          this.otherInfoGroup.Location = new Point(0, 208);          this.otherInfoGroup.Name = "otherInfoGroup";          this.otherInfoGroup.Size = new Size(304, 72);          this.otherInfoGroup.TabIndex = 3;          this.otherInfoGroup.TabStop = false;          this.otherInfoGroup.Text = "Other Information";          //          // NoRadioButton          //          this.NoRadioButton.Location = new Point(184, 40);          this.NoRadioButton.Name = "NoRadioButton";          this.NoRadioButton.Size = new Size(56, 24);          this.NoRadioButton.TabIndex = 12;          this.NoRadioButton.Text = "No";          //          // YesRadioButton          //          this.YesRadioButton.Location = new Point(112, 40);          this.YesRadioButton.Name = "YesRadioButton";          this.YesRadioButton.Size = new Size(56, 24);          this.YesRadioButton.TabIndex = 11;          this.YesRadioButton.Text = "Yes";          //          // Over35Box          //          this.Over35Box.Location = new Point(240, 16);          this.Over35Box.Name = "Over35Box";          this.Over35Box.Size = new Size(80, 24);          this.Over35Box.TabIndex = 10;          this.Over35Box.Text = "Over 35";          //          // B1835Box          //          this.B1835Box.Location = new Point(184, 16);          this.B1835Box.Name = "B1835Box";          this.B1835Box.Size = new Size(80, 24);          this.B1835Box.TabIndex = 9;          this.B1835Box.Text = "18 - 35";          //          // Under18Box          //          this.Under18Box.Location = new Point(112, 16);          this.Under18Box.Name = "Under18Box";          this.Under18Box.Size = new Size(80, 24);          this.Under18Box.TabIndex = 8;          this.Under18Box.Text = "Under 18";          //          // marriedLabel          //          this.marriedLabel.Location = new Point(8, 40);          this.marriedLabel.Name = "marriedLabel";          this.marriedLabel.Size = new Size(104, 16);          this.marriedLabel.TabIndex = 7;          this.marriedLabel.Text = "Married?";          //          // ageGroupLabel          //          this.ageGroupLabel.Location = new Point(8, 16);          this.ageGroupLabel.Name = "ageGroupLabel";          this.ageGroupLabel.Size = new Size(104, 16);          this.ageGroupLabel.TabIndex = 0;          this.ageGroupLabel.Text = "Age Group";          //          // basicDetailGroup          //          this.basicDetailGroup.Controls.Add(this.lastNameTextBox);          this.basicDetailGroup.Controls.Add(this.lastNameLabel);          this.basicDetailGroup.Controls.Add(this.miTextBox);          this.basicDetailGroup.Controls.Add(this.middleInitialLabel);          this.basicDetailGroup.Controls.Add(this.firstNameTextBox);          this.basicDetailGroup.Controls.Add(this.firstNameLabel);          this.basicDetailGroup.Location = new Point(0, 24);          this.basicDetailGroup.Name = "basicDetailGroup";          this.basicDetailGroup.Size = new Size(304, 88);          this.basicDetailGroup.TabIndex = 1;          this.basicDetailGroup.TabStop = false;          this.basicDetailGroup.Text = "Basic Details";          //          // lastNameTextBox          //          this.lastNameTextBox.Location = new Point(120, 64);          this.lastNameTextBox.Name = "lastNameTextBox";          this.lastNameTextBox.Size = new Size(176, 20);          this.lastNameTextBox.TabIndex = 3;          this.lastNameTextBox.Text = "";          //          // lastNameLabel          //          this.lastNameLabel.Location = new Point(8, 64);          this.lastNameLabel.Name = "lastNameLabel";          this.lastNameLabel.Size = new Size(104, 16);          this.lastNameLabel.TabIndex = 4;          this.lastNameLabel.Text = "Last Name";          //          // miTextBox          //          this.miTextBox.Location = new Point(120, 38);          this.miTextBox.MaxLength = 1;          this.miTextBox.Name = "miTextBox";          this.miTextBox.Size = new Size(16, 20);          this.miTextBox.TabIndex = 2;          this.miTextBox.Text = "";          //          // middleInitialLabel          //          this.middleInitialLabel.Location = new Point(8, 38);          this.middleInitialLabel.Name = "middleInitialLabel";          this.middleInitialLabel.Size = new Size(104, 16);          this.middleInitialLabel.TabIndex = 2;          this.middleInitialLabel.Text = "Middle Initial";          //          // firstNameTextBox          //          this.firstNameTextBox.Location = new Point(120, 16);          this.firstNameTextBox.Name = "firstNameTextBox";          this.firstNameTextBox.Size = new Size(176, 20);          this.firstNameTextBox.TabIndex = 1;          this.firstNameTextBox.Text = "";          //          // firstNameLabel          //          this.firstNameLabel.Location = new Point(8, 16);          this.firstNameLabel.Name = "firstNameLabel";          this.firstNameLabel.Size = new Size(104, 16);          this.firstNameLabel.TabIndex = 0;          this.firstNameLabel.Text = "First Name";          //          // CustomerEntryForm          //          this.AutoScaleBaseSize = new Size(5, 13);          this.ClientSize = new Size(312, 285);          this.Controls.Add(this.otherInfoGroup);          this.Controls.Add(this.addressGroup);          this.Controls.Add(this.basicDetailGroup);          this.Controls.Add(this.formLabel);          this.Name = "CustomerEntryForm";          this.Text = "Customer Entry Application";          this.addressGroup.ResumeLayout(false);          this.otherInfoGroup.ResumeLayout(false);          this.basicDetailGroup.ResumeLayout(false);          this.ResumeLayout(false);       }       [STAThread]       static void Main()       {          Application.Run(new CustomerEntryForm());       }    } } 
Figure 7.7. Using basic form controls.

Now, notice the enriched user interface that can be achieved by using the Tab control to lay out components of the customer entry form (Listing 7.4, Figure 7.8) as individual tab pages.

Listing 7.4 Using Tab Control
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CustomerEntryApplication2 {    public class Form1 : Form    {       private TabControl tabControl1;       private TabPage tabPage1;       private TabPage tabPage2;       private TabPage tabPage3;       private Label formLabel;       private TextBox lastNameTextBox;       private Label lastNameLabel;       private TextBox miTextBox;       private Label middleInitialLabel;       private TextBox firstNameTextBox;       private Label firstNameLabel;       private TextBox zipCodeBox;       private ComboBox stateBox;       private Label stateLabel;       private TextBox cityBox;       private Label cityLabel;       private TextBox streetTextBox;       private Label streetLabel;       private RadioButton NoRadioButton;       private RadioButton YesRadioButton;       private CheckBox Over35Box;       private CheckBox B1835Box;       private CheckBox Under18Box;       private Label marriedLabel;       private Label ageGroupLabel;       private Button NewButton;       private Button UpdateButton;       private Button DeleteButton;       private System.ComponentModel.Container components = null;       public Form1()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          if( disposing )          {             if (components != null)             {                components.Dispose();             }          }          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.tabControl1 = new TabControl();          this.tabPage1 = new TabPage();          this.tabPage2 = new TabPage();          this.tabPage3 = new TabPage();          this.formLabel = new Label();          this.lastNameTextBox = new TextBox();          this.lastNameLabel = new Label();          this.miTextBox = new TextBox();          this.middleInitialLabel = new Label();          this.firstNameTextBox = new TextBox();          this.firstNameLabel = new Label();          this.zipCodeBox = new TextBox();          this.stateBox = new ComboBox();          this.stateLabel = new Label();          this.cityBox = new TextBox();          this.cityLabel = new Label();          this.streetTextBox = new TextBox();          this.streetLabel = new Label();          this.NoRadioButton = new RadioButton();          this.YesRadioButton = new RadioButton();          this.Over35Box = new CheckBox();          this.B1835Box = new CheckBox();          this.Under18Box = new CheckBox();          this.marriedLabel = new Label();          this.ageGroupLabel = new Label();          this.NewButton = new Button();          this.UpdateButton = new Button();          this.DeleteButton = new Button();          this.tabControl1.SuspendLayout();          this.tabPage1.SuspendLayout();          this.tabPage2.SuspendLayout();          this.tabPage3.SuspendLayout();          this.SuspendLayout();          //          // tabControl1          //          this.tabControl1.Controls.Add(this.tabPage1);          this.tabControl1.Controls.Add(this.tabPage2);          this.tabControl1.Controls.Add(this.tabPage3);          this.tabControl1.Location = new Point(0, 16);          this.tabControl1.Name = "tabControl1";          this.tabControl1.SelectedIndex = 0;          this.tabControl1.Size = new Size(312, 136);          this.tabControl1.TabIndex = 10;          //          // tabPage1          //          this.tabPage1.Controls.Add(this.lastNameTextBox);          this.tabPage1.Controls.Add(this.lastNameLabel);          this.tabPage1.Controls.Add(this.miTextBox);          this.tabPage1.Controls.Add(this.middleInitialLabel);          this.tabPage1.Controls.Add(this.firstNameTextBox);          this.tabPage1.Controls.Add(this.firstNameLabel);          this.tabPage1.Location = new Point(4, 22);          this.tabPage1.Name = "tabPage1";          this.tabPage1.Size = new Size(304, 110);          this.tabPage1.TabIndex = 0;          this.tabPage1.Text = "Basic";          //          // tabPage2          //          this.tabPage2.Controls.Add(this.zipCodeBox);          this.tabPage2.Controls.Add(this.stateBox);          this.tabPage2.Controls.Add(this.stateLabel);          this.tabPage2.Controls.Add(this.cityBox);          this.tabPage2.Controls.Add(this.cityLabel);          this.tabPage2.Controls.Add(this.streetTextBox);          this.tabPage2.Controls.Add(this.streetLabel);          this.tabPage2.Location = new Point(4, 22);          this.tabPage2.Name = "tabPage2";          this.tabPage2.Size = new Size(304, 110);          this.tabPage2.TabIndex = 1;          this.tabPage2.Text = "Address";          //          // tabPage3          //          this.tabPage3.Controls.Add(this.NoRadioButton);          this.tabPage3.Controls.Add(this.YesRadioButton);          this.tabPage3.Controls.Add(this.Over35Box);          this.tabPage3.Controls.Add(this.B1835Box);          this.tabPage3.Controls.Add(this.Under18Box);          this.tabPage3.Controls.Add(this.marriedLabel);          this.tabPage3.Controls.Add(this.ageGroupLabel);          this.tabPage3.Location = new Point(4, 22);          this.tabPage3.Name = "tabPage3";          this.tabPage3.Size = new Size(304, 110);          this.tabPage3.TabIndex = 2;          this.tabPage3.Text = "Other";          //          // formLabel          //          this.formLabel.Dock = DockStyle.Top;          this.formLabel.Font = new Font("Microsoft Sans Serif",                             8.25F, FontStyle.Bold, GraphicsUnit.Point, ((Byte)(0)));          this.formLabel.Location = new Point(0, 0);          this.formLabel.Name = "formLabel";          this.formLabel.Size = new Size(312, 16);          this.formLabel.TabIndex = 1;          this.formLabel.Text = "Customer Entry Form";          this.formLabel.TextAlign = ContentAlignment.TopCenter;          //          // lastNameTextBox          //          this.lastNameTextBox.Location = new Point(120, 72);          this.lastNameTextBox.Name = "lastNameTextBox";          this.lastNameTextBox.Size = new Size(176, 20);          this.lastNameTextBox.TabIndex = 4;          this.lastNameTextBox.Text = "";          //          // lastNameLabel          //          this.lastNameLabel.Location = new Point(8, 72);          this.lastNameLabel.Name = "lastNameLabel";          this.lastNameLabel.Size = new Size(104, 16);          this.lastNameLabel.TabIndex = 10;          this.lastNameLabel.Text = "Last Name";          //          // miTextBox          //          this.miTextBox.Location = new Point(120, 48);          this.miTextBox.MaxLength = 1;          this.miTextBox.Name = "miTextBox";          this.miTextBox.Size = new Size(16, 20);          this.miTextBox.TabIndex = 3;          this.miTextBox.Text = "";          //          // middleInitialLabel          //          this.middleInitialLabel.Location = new Point(8, 48);          this.middleInitialLabel.Name = "middleInitialLabel";          this.middleInitialLabel.Size = new Size(104, 16);          this.middleInitialLabel.TabIndex = 7;          this.middleInitialLabel.Text = "Middle Initial";          //          // firstNameTextBox          //          this.firstNameTextBox.Location = new Point(120, 24);          this.firstNameTextBox.Name = "firstNameTextBox";          this.firstNameTextBox.Size = new Size(176, 20);          this.firstNameTextBox.TabIndex = 2;          this.firstNameTextBox.Text = "";          //          // firstNameLabel          //          this.firstNameLabel.Location = new Point(8, 24);          this.firstNameLabel.Name = "firstNameLabel";          this.firstNameLabel.Size = new Size(104, 16);          this.firstNameLabel.TabIndex = 5;          this.firstNameLabel.Text = "First Name";          //          // zipCodeBox          //          this.zipCodeBox.Location = new Point(216, 69);          this.zipCodeBox.MaxLength = 5;          this.zipCodeBox.Name = "zipCodeBox";          this.zipCodeBox.Size = new Size(72, 20);          this.zipCodeBox.TabIndex = 9;          this.zipCodeBox.Text = "";          //          // stateBox          //          this.stateBox.Items.AddRange(new object[] {"PA","NJ","NY"});          this.stateBox.Location = new Point(120, 69);          this.stateBox.Name = "stateBox";          this.stateBox.Size = new Size(40, 21);          this.stateBox.TabIndex = 8;          //          // stateLabel          //          this.stateLabel.Location = new Point(8, 69);          this.stateLabel.Name = "stateLabel";          this.stateLabel.Size = new Size(104, 16);          this.stateLabel.TabIndex = 10;          this.stateLabel.Text = "State";          //          // cityBox          //          this.cityBox.Location = new Point(120, 43);          this.cityBox.MaxLength = 100;          this.cityBox.Name = "cityBox";          this.cityBox.Size = new Size(72, 20);          this.cityBox.TabIndex = 7;          this.cityBox.Text = "";          //          // cityLabel          //          this.cityLabel.Location = new Point(8, 43);          this.cityLabel.Name = "cityLabel";          this.cityLabel.Size = new Size(104, 16);          this.cityLabel.TabIndex = 9;          this.cityLabel.Text = "City";          //          // streetTextBox          //          this.streetTextBox.Location = new Point(120, 21);          this.streetTextBox.Name = "streetTextBox";          this.streetTextBox.Size = new Size(176, 20);          this.streetTextBox.TabIndex = 6;          this.streetTextBox.Text = "";          //          // streetLabel          //          this.streetLabel.Location = new Point(8, 21);          this.streetLabel.Name = "streetLabel";          this.streetLabel.Size = new Size(104, 16);          this.streetLabel.TabIndex = 8;          this.streetLabel.Text = "Street";          //          // NoRadioButton          //          this.NoRadioButton.Location = new Point(172, 55);          this.NoRadioButton.Name = "NoRadioButton";          this.NoRadioButton.Size = new Size(56, 24);          this.NoRadioButton.TabIndex = 15;          this.NoRadioButton.Text = "No";          //          // YesRadioButton          //          this.YesRadioButton.Location = new Point(100, 55);          this.YesRadioButton.Name = "YesRadioButton";          this.YesRadioButton.Size = new Size(56, 24);          this.YesRadioButton.TabIndex = 14;          this.YesRadioButton.Text = "Yes";          //          // Over35Box          //          this.Over35Box.Location = new Point(228, 31);          this.Over35Box.Name = "Over35Box";          this.Over35Box.Size = new Size(80, 24);          this.Over35Box.TabIndex = 13;          this.Over35Box.Text = "Over 35";          //          // B1835Box          //          this.B1835Box.Location = new Point(172, 31);          this.B1835Box.Name = "B1835Box";          this.B1835Box.Size = new Size(80, 24);          this.B1835Box.TabIndex = 12;          this.B1835Box.Text = "18 - 35";          //          // Under18Box          //          this.Under18Box.Location = new Point(100, 31);          this.Under18Box.Name = "Under18Box";          this.Under18Box.Size = new Size(80, 24);          this.Under18Box.TabIndex = 11;          this.Under18Box.Text = "Under 18";          //          // marriedLabel          //          this.marriedLabel.Location = new Point(8, 55);          this.marriedLabel.Name = "marriedLabel";          this.marriedLabel.Size = new Size(104, 16);          this.marriedLabel.TabIndex = 14;          this.marriedLabel.Text = "Married?";          //          // ageGroupLabel          //          this.ageGroupLabel.Location = new Point(8, 31);          this.ageGroupLabel.Name = "ageGroupLabel";          this.ageGroupLabel.Size = new Size(104, 16);          this.ageGroupLabel.TabIndex = 13;          this.ageGroupLabel.Text = "Age Group";          //          // NewButton          //          this.NewButton.Location = new Point(8, 160);          this.NewButton.Name = "NewButton";          this.NewButton.Size = new Size(96, 24);          this.NewButton.TabIndex = 2;          this.NewButton.Text = "New";          //          // UpdateButton          //          this.UpdateButton.Location = new Point(112, 160);          this.UpdateButton.Name = "UpdateButton";          this.UpdateButton.Size = new Size(96, 24);          this.UpdateButton.TabIndex = 3;          this.UpdateButton.Text = "Update";          //          // DeleteButton          //          this.DeleteButton.Location = new Point(216, 160);          this.DeleteButton.Name = "DeleteButton";          this.DeleteButton.Size = new Size(96, 24);          this.DeleteButton.TabIndex = 4;          this.DeleteButton.Text = "Delete";          //          // Form1          //          this.AutoScaleBaseSize = new Size(5, 13);          this.ClientSize = new Size(312, 189);          this.Controls.Add(this.DeleteButton);          this.Controls.Add(this.UpdateButton);          this.Controls.Add(this.NewButton);          this.Controls.Add(this.formLabel);          this.Controls.Add(this.tabControl1);          this.Name = "Form1";          this.Text = "Customer Entry Application";          this.tabControl1.ResumeLayout(false);          this.tabPage1.ResumeLayout(false);          this.tabPage2.ResumeLayout(false);          this.tabPage3.ResumeLayout(false);          this.ResumeLayout(false);       }       [STAThread]       static void Main()       {          Application.Run(new Form1());       }    } } 
Figure 7.8. Using Tab control and tab pages.

Apart from tabs, using hierarchical information can also provide a further-enhanced user interface for displaying data. For instance, Listing 7.5 shows an office information application (Figure 7.9) that allows the user to navigate to a particular office (from a list of locations organized by continent , country, state, and city hierarchy) to get further details. The Tree View control is used in this example.

Listing 7.5 Using the Tree Control
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace OfficeApplication {    public class OfficeAddressApplication : Form    {       private TreeView treeView1;       private TextBox addressBox;       private System.ComponentModel.Container components = null;       public OfficeAddressApplication()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          if( disposing )          {             if (components != null)             {                components.Dispose();             }          }          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.treeView1 = new TreeView();          this.addressBox = new TextBox();          this.SuspendLayout();          //          // treeView1          //          this.treeView1.ImageIndex = -1;          this.treeView1.Location = new Point(16, 24);          this.treeView1.Name = "treeView1";          this.treeView1.Nodes.AddRange(new TreeNode[] {             new TreeNode("North America", new TreeNode[] {                                   new TreeNode("United States", new TreeNode[] {                                    new TreeNode("New York"),                                    new TreeNode("New Jersey",                                       new TreeNode[] {                                           new TreeNode("Princeton"),                                           new TreeNode("Edison")}                                       ),                                    new TreeNode("California")                                   }                               ),                               new TreeNode("Canada")})});          this.treeView1.SelectedImageIndex = -1;          this.treeView1.Size = new Size(264, 104);          this.treeView1.TabIndex = 0;          this.treeView1.AfterSelect +=              new TreeViewEventHandler(treeView1_AfterSelect);          //          // addressBox          //          this.addressBox.Location = new Point(16, 144);          this.addressBox.Multiline = true;          this.addressBox.Name = "addressBox";          this.addressBox.Size = new Size(264, 120);          this.addressBox.TabIndex = 1;          this.addressBox.Text = "";          //          // OfficeAddressApplication          //          this.AutoScaleBaseSize = new Size(5, 13);          this.ClientSize = new Size(292, 273);          this.Controls.Add(this.addressBox);          this.Controls.Add(this.treeView1);          this.Name = "OfficeAddressApplication";          this.Text = "Office Address Finder Application";          this.ResumeLayout(false);       }       [STAThread]       static void Main()       {          Application.Run(new OfficeAddressApplication());       }       private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)       {          if (e.Node.Text.Equals("Princeton"))          {             addressBox.Text = "Address of "+e.Node.Text+ " Office"                +" ...";          }          else          {             addressBox.Text = "";          }       }    } } 
Figure 7.9. Using Tree View control.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net