oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

A Template for C# Windows Applications
Pages: 1, 2, 3, 4

Adding Controls to the Form

Now that you have a form, its time to add controls to it. Windows controls are inherited from the System.Windows.Forms.Control class, which handles user input (through the keyboard and pointing devices), message routing, and security. It defines the bounds of a control (its position and size), although it does not implement painting. It provides a window handle (hWnd). Some important properties include Name, Enabled, Text, BackColor, Left, Top, Size, Location, Visible, Width, and Height.

The System.Windows.Forms namespace provides dozens of such controls. Each control has its own property and characteristic, and it's impossible to discuss them all in this article; what is shown here is how easy it is to add a control to your form. In the code example that accompanies this article, three controls are added: Label, Button, and TreeView.

The first step in adding a control is, of course, to declare the control in your class.

Label label;
Button button;
TreeView tree;

For brevity, you can instantiate the objects in the same line in which you declare them.

  Label label = new Label();
  Button button = new Button();
  TreeView tree = new TreeView();

Then, in the InitializeComponent method, you set some properties of these controls. Typically, you want to set a control's dimension and location in the form. For its dimension, you can use its Width and Height properties, just as you would in the form. For example, we set the TreeView control's dimension by using these properties.

    tree.Width = 100;
    tree.Height = 100;

However, the .NET Framework class library provides the System.Drawing.Size structure that you can instantiate and assign to the Size property of your control. For instance, the dimensions of the the Label and Button controls are controlled this way.

label.Size = new Size(160, 30);
button.Size = new Size(100, 30);

For the location, you can use a control's Left and Top properties to decide the location of the top-left corner of the control. This is what we do to the TreeView control in Listing 1.

tree.Top = 40;
tree.Left = 20;

Alternatively, you can make your code shorter by using the Location property, assigning it an instance of the System.Drawing.Point structure. We do this to the Label and Button controls.

label.Location = new Point(220, 40);
button.Location = new Point(220, 80);

Next, to make the controls visible on the form, you add each control to the ControlCollection of the form. Use the Add method of the Form.ControlCollection class to add a control. The ControlCollection of a form can be accessed using the Controls property of the form. Therefore, to add controls, we write the following code.


Adding Menu and Menu Items

It is very hard to find a decent Windows application that has no menu. Menus make it easy to access certain functionality in the application and, in most circumstances, can minimize the use of controls. Using menus is often preferable to using controls; menus take less space and make your application look more organized.

In the System.Windows.Forms namespace, all menu-related controls are child classes of the Menu class. This class is an abstract class, however, so you cannot directly instantiate it. The Menu class has three child classes:

  • ContextMenu
  • MainMenu
  • MenuItem

The ContextMenu class represents shortcut menus that can be displayed when the user clicks the right mouse button over a control or area of the form. Shortcut menus are typically used to combine different menu items from a MainMenu of a form that are useful for the user, given the context of the application.

The MainMenu class represents the "conventional" menu on the top part of your form. It is a container for the menu structure of a form. A menu is composed of menu items represented by the MenuItem class. Each menu item is normally a command for your application or a parent menu for other submenu items. The Form class has the Menu property, to which you can assign a MainMenu object to bind the MainMenu object to the form.

In this template, we don't use the ContextMenu class; however, we do demonstrate how to use the MainMenu and MenuItem classes.

The first thing you need to do to have a menu in your form is add a MainMenu object to the form.

MainMenu mainMenu = new MainMenu();

Nothing is visible in the MainMenu object, however, until you add a MenuItem object. In Listing 1, the main menu item is called fileMenuItem, and its Text property is given the value of &File. The ampersand character is used to indicate the character that is to be underlined when the Alt key is pressed. Adding a MenuItem object to a MainMenu or another MenuItem object is done by calling the Add method of the MenuItemCollection of the Menu object. This collection can be accessed from the MenuItems property of the Menu class.

MenuItem fileMenuItem = new MenuItem();

There are also other MenuItem objects that are added to the fileMenuItem menu item. These MenuItem objects become the submenu of fileMenuItem. You can then add other menu items to a submenu. Figure 2 shows the hierarchical structure of these menu items.

Figure 2. Hierarchical structure of menu items.

The declaration of the menu items in Listing 1 is as follows.

  MenuItem fileNewMenuItem;
  MenuItem fileOpenMenuItem;
  MenuItem fileSaveMenuItem;
  MenuItem fileSaveAsMenuItem;
  MenuItem fileMenuWithSubmenu;
  MenuItem submenuMenuItem;
  MenuItem fileExitMenuItem;

The MenuItem class has several constructors, some of which allow you to instantiate a MenuItem object and pass a handler for some event, as well as assign a shortcut for the menu item, as we do in the following code in Listing 1.

    // the following constructor is the same as:
    //   menuItem fileNewMenuItem = new MenuItem();
    //   fileNewMenuItem.Text = "&New";
    //   fileNewMenuItem.Shortcut = Shortcut.CtrlN;
    //   fileNewMenuItem.Click += new    
    //     System.EventHandler(this.fileNewMenuItem_Click);
    fileNewMenuItem = new MenuItem("&New", 
      new System.EventHandler(this.fileNewMenuItem_Click), Shortcut.CtrlN);

    fileOpenMenuItem = new MenuItem("&Open", 
      new System.EventHandler(this.fileOpenMenuItem_Click), Shortcut.CtrlO);
    fileSaveMenuItem = new MenuItem("&Save", 
      new System.EventHandler(this.fileSaveMenuItem_Click), Shortcut.CtrlS);

    fileSaveAsMenuItem = new MenuItem("Save &As", 
      new System.EventHandler(this.fileSaveAsMenuItem_Click));

    fileMenuWithSubmenu = new MenuItem("&With Submenu");

    submenuMenuItem = new MenuItem("Su&bmenu", 
      new System.EventHandler(this.submenuMenuItem_Click));

    fileExitMenuItem = new MenuItem("E&xit", 
      new System.EventHandler(this.fileExitMenuItem_Click));

Event handling is discussed further in the section "Adding Event Handlers."

As mentioned, the menu items are added to the fileMenuItem control.

    fileMenuItem.MenuItems.Add("-"); // add a separator

Note that you can create a separator between menu items by passing a - character to the Add method of the MenuItems collection of the Menu class.

Pages: 1, 2, 3, 4

Next Pagearrow