ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


A Template for C# Windows Applications

by Budi Kurniawan
05/13/2002

A Windows application normally comes with some standard features: controls, menus, one or more toolbars, and a status bar with one or more panels. Every time you start to write a new Windows application, you often start with the same ritual: creating the above components and adding event handlers for them. You can save several hours of work if you work from a template that already defines these standard components.

This article offers such a template, and also also provides some essential knowledge of C# Windows programming with the Microsoft .NET Framework class library. The template presented here is especially useful if you're not using an IDE like Visual Studio .NET. If you are, you can still get some benefits by learning how everything works beneath it. The template itself is given in Listing 1 at the end of the article.

When writing a Windows application, here are the steps you normally take as preparation, before you start coding your business logic.

  1. Create a form.
  2. Add controls to the form.
  3. Add a menu and menu items and bind the menu to the form.
  4. Create a toolbar.
  5. Add a status bar.
  6. Add event handlers.

With the template provided in this project, however, you can skip these steps entirely -- you just need to make some modifications here and there. The "raw" Windows application provided by this template is shown in Figure 1.


Figure 1. The Windows application template.

Creating A Form

In .NET Framework programming, a form is always a child of the System.Windows.Forms.Form class. Inheriting this class, without even adding an additional line of code, is sufficient to create a blank window. Therefore, if your class is named MyWinApp, here is the class definition in C# that inherits the Form class. Notice that you use a colon to specify inheritance.


using System.Windows.Forms;

public class MyWinApp: Form {

}

Like any other application, you need a static Main method as an entry point to your program. In the Main method, you can use the Run method of the System.Windows.Forms.Application class to start your application and show the form. The Run method has three overloads. For a Windows application, you use the overload that accepts a Form object.


public static void Main() {
    MyWinApp form = new MyWinApp();
    Application.Run(form);
  }

Alternatively, you can write the two lines of code in the Main method in one single line.


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

Setting Properties

Once you have a form, you may want to set its properties (such as width, height and title) and assign it a custom icon like the one shown in Figure 1. You can place the code that does this in your constructor, or in another method that is called from the constructor. In the template given in Listing 1, there is a method called InitializeComponent that is called from the class's constructor. You write initialization code in this InitializeComponent method.

For its dimension, specify the number of pixels for the Form class's Width and Height properties:


    this.Width = 400;
    this.Height = 300;

For the title, you use the Text property of the Form class.


this.Text = "My Windows Application";

Setting the Icon

If you don't specify one, a Windows application will show a default icon. You can use your own icon by setting the form's Icon property. This property accepts a System.Drawing.Icon object. The Icon class has several constructors you can use to instantiate an Icon object. In the template given in Example 1, I use the constructor that accepts the path to an .ico file, passing the complete path to an icon file named applicationLogo.ico.


    this.Icon = new Icon(imageFolder + "applicationLogo.ico");

where imageFolder is a static field that specifies the directory of the icon file. Note that imageFolder is defined as follows:


static String imageFolder = "Images" + Path.DirectorySeparatorChar.ToString();

This tells the application that the icon file is to be found in the Images directory, under the application directory. I used the System.IO.Path class's DirectorySeparatorChar property to obtain the character the operating system uses to separate a directory from its parent. I did not hard-code it by using a backslash, which is the valid directory separator character for Windows operating systems, in order to make sure that the template is portable to other operating systems, such as Linux and Unix. Yes, if you're wondering, .NET Framework has been ported to Linux, in some projects such as Ximian's Mono and Portable .NET.

Positioning the Form

It's useful to center your form. For this, you should use the StartPosition property and assign it one of the members of the FormStartPosition enumeration. To center your form, use the following code:


this.StartPosition = FormStartPosition.CenterScreen;

Note that you can also center your form by using the CenterToScreen method of the Form class; however, this method is not intended to be used directly from your code.


this.CenterToScreen();

The Form class also has some other interesting properties and methods. Here are some of the things you can do:

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.


this.Controls.Add(label);
this.Controls.Add(button);
this.Controls.Add(tree);

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:

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();
mainMenu.MenuItems.Add(fileMenuItem);

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(fileNewMenuItem);
    fileMenuItem.MenuItems.Add(fileOpenMenuItem);
    fileMenuItem.MenuItems.Add(fileSaveMenuItem);
    fileMenuItem.MenuItems.Add(fileSaveAsMenuItem);
    fileMenuItem.MenuItems.Add(fileMenuWithSubmenu);
    fileMenuWithSubmenu.MenuItems.Add(submenuMenuItem);
    fileMenuItem.MenuItems.Add("-"); // add a separator
    fileMenuItem.MenuItems.Add(fileExitMenuItem);

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.

Creating A Toolbar

To make your application more user-friendly, you can add a toolbar to your form. A toolbar is represented by the System.Windows.Forms.ToolBar class. You can have more than one toolbar in a form, if you wish. A toolbar contains one or more buttons, represented by the ToolBarButton class. You normally embed an image or an icon as the display for each toolbar button. (See Figure 1). For this purpose, you need to have an ImageList control that acts as the container of the images. Here is the declaration and instantiation of the ImageList object.


ImageList imageList = new ImageList();

The archive that accompanies this article contains four image files (.bmp and .gif files) that you can use for your toolbar buttons. First, you need to instantiate an Image object for each image file and add these images to the ImageList control. The Image and Bitmap classes can be found in the System.Drawing namespace.


  Image newFileImage = new Bitmap(imageFolder + "newFile.bmp");
  Image openFileImage = new Bitmap(imageFolder + "openFile.gif");
  Image saveFileImage = new Bitmap(imageFolder + "saveFile.bmp");
  Image printImage = new Bitmap(imageFolder + "print.gif");
  .
  .
  .
  imageList.Images.Add(newFileImage);
  imageList.Images.Add(openFileImage);
  imageList.Images.Add(saveFileImage);
  imageList.Images.Add(printImage);

Notice that you can add an Image object to an ImageList control by using the Add method of its Images collection.

Now, for the toolbar buttons to use these images, you must first assign that ImageList control to the ImageList property of the ToolBar control.


toolBar.ImageList = imageList;

Then, use the ImageIndex of each individual toolbar button to assign an image in the ImageList control.


newToolBarButton.ImageIndex = 0;
openToolBarButton.ImageIndex = 1;
saveToolBarButton.ImageIndex = 2;
printToolBarButton.ImageIndex = 3;

Just like that. Of course, you then have to add the toolbar buttons to the ToolBar control.


    toolBar.Buttons.Add(separatorToolBarButton);
    toolBar.Buttons.Add(newToolBarButton);
    toolBar.Buttons.Add(openToolBarButton);
    toolBar.Buttons.Add(saveToolBarButton);
    toolBar.Buttons.Add(separatorToolBarButton);
    toolBar.Buttons.Add(printToolBarButton);

And finally, add the ToolBar control to the form.


this.Controls.Add(toolBar);

Adding A Status Bar

A status bar is often overlooked, and is sometimes not considered a significant feature of a Windows application; having one is always nice, though. A status bar is represented by the System.Windows.Forms.StatusBar class. It provides properties that enable you to customize the appearance of the control. Typically a StatusBar control consists of StatusBarPanel objects, each of which displays text and/or an icon. In our template, the status bar has two panels.


  StatusBar statusBar = new StatusBar();
  StatusBarPanel statusBarPanel1 = new StatusBarPanel();
  StatusBarPanel statusBarPanel2 = new StatusBarPanel();

The properties of the status bar and status bar panels are set in the following code.


    statusBarPanel1.BorderStyle = StatusBarPanelBorderStyle.Sunken;
    statusBarPanel1.Text = "Press F1 for Help";
    statusBarPanel1.AutoSize = StatusBarPanelAutoSize.Spring;
    statusBarPanel2.BorderStyle = StatusBarPanelBorderStyle.Raised;
    statusBarPanel2.ToolTipText = System.DateTime.Now.ToShortTimeString();
    statusBarPanel2.Text = System.DateTime.Today.ToLongDateString();
    statusBarPanel2.AutoSize = StatusBarPanelAutoSize.Contents;
    statusBar.ShowPanels = true;
    statusBar.Panels.Add(statusBarPanel1);
    statusBar.Panels.Add(statusBarPanel2);

As always, you need to add this status bar to the form.


this.Controls.Add(statusBar);

Adding Event Handlers

Adding event handlers is one of the most important tasks in Windows programming. Event handling guarantees that your application can interact with its users and perform other important functions. In C#, you assign an event handler to an event of a control (or menu item, or toolbar button) by adding an event delegate to the event you want to capture. For instance, the following code assigns the button_Click event handler to the Click event of a Button control.


button.Click += new System.EventHandler(this.button_Click);

The button_Click event handler must then be present.


  private void button_Click(Object sender, System.EventArgs e) {
    MessageBox.Show("Thank you.", "The Event Information");
  }

MenuItem objects can be assigned an event handler at the same time they are instantiated.


    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));


You cannot assign an event handler to a toolbar button, but you can assign one to the containing toolbar. Then you can detect which button is being clicked.


toolBar.ButtonClick += new ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);

protected void toolBar_ButtonClick(Object sender, ToolBarButtonClickEventArgs e) {
    
    // Evaluate the Button property to determine which button was clicked.
    switch (toolBar.Buttons.IndexOf(e.Button)) {
      case 1:
        MessageBox.Show("Second button.", "The Event Information");
        break;
      case 2:
        MessageBox.Show("third button", "The Event Information");
        break;
      case 3:
        MessageBox.Show("fourth button.", "The Event Information");
        break;
    }
  }        

The code in Example 1 also provides an event handler for the Closing event of the form. By overriding the OnClosing method, you can intercept the user's click on the X button of your form. You can then cancel the closing if you desire.


  protected override void OnClosing(CancelEventArgs e) {
    MessageBox.Show("Exit now.", "The Event Information");
  }

The Template

Finally, here is the template that you can use as a starting point for your C# Windows application. The whole application, including the image files, can be downloaded here.

Listing 1. A C# Windows Application Template


/*
  to compile this source file, type
    csc MyWinApp.cs
*/

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

public class MyWinApp: Form {

  Label label = new Label();
  Button button = new Button();
  TreeView tree = new TreeView();
  ImageList imageList = new ImageList();
  static String imageFolder = "Images" + Path.DirectorySeparatorChar.ToString();

  // -------------- Images declarations ------------------------------------
  Image newFileImage = new Bitmap(imageFolder + "newFile.bmp");
  Image openFileImage = new Bitmap(imageFolder + "openFile.gif");
  Image saveFileImage = new Bitmap(imageFolder + "saveFile.bmp");
  Image printImage = new Bitmap(imageFolder + "print.gif");

  // -------------- End of Images declaration ------------------------------------

  // -------------- menu ------------------------------------
  MainMenu mainMenu = new MainMenu();

  MenuItem fileMenuItem = new MenuItem();
  MenuItem fileNewMenuItem;
  MenuItem fileOpenMenuItem;
  MenuItem fileSaveMenuItem;
  MenuItem fileSaveAsMenuItem;
  MenuItem fileMenuWithSubmenu;
  MenuItem submenuMenuItem;
  MenuItem fileExitMenuItem;

  // -------------- End of menu ------------------------------------
  
  // -------------- Toolbar ------------------------------------
  ToolBar toolBar = new ToolBar();
  ToolBarButton separatorToolBarButton = new ToolBarButton();
  ToolBarButton newToolBarButton = new ToolBarButton();
  ToolBarButton openToolBarButton = new ToolBarButton();
  ToolBarButton saveToolBarButton = new ToolBarButton();
  ToolBarButton printToolBarButton = new ToolBarButton();

  // -------------- End of Toolbar ------------------------------------

  // -------------- StatusBar ------------------------------------
  StatusBar statusBar = new StatusBar();

  StatusBarPanel statusBarPanel1 = new StatusBarPanel();
  StatusBarPanel statusBarPanel2 = new StatusBarPanel();

  // -------------- End of StatusBar ------------------------------------
 

  public MyWinApp() {
    InitializeComponent();
  }

  private void InitializeComponent() {
    this.Text = "My Windows Application";
    this.Icon = new Icon(imageFolder + "applicationLogo.ico");
    this.Width = 400;
    this.Height = 300;
    this.StartPosition = FormStartPosition.CenterScreen;

    imageList.Images.Add(newFileImage);
    imageList.Images.Add(openFileImage);
    imageList.Images.Add(saveFileImage);
    imageList.Images.Add(printImage);


    // menu
    fileMenuItem.Text = "&File";

    // 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));


    mainMenu.MenuItems.Add(fileMenuItem);
    fileOpenMenuItem.Checked = true;
    fileMenuItem.MenuItems.Add(fileNewMenuItem);
    fileMenuItem.MenuItems.Add(fileOpenMenuItem);
    fileMenuItem.MenuItems.Add(fileSaveMenuItem);
    fileMenuItem.MenuItems.Add(fileSaveAsMenuItem);
    fileMenuItem.MenuItems.Add(fileMenuWithSubmenu);
    fileMenuWithSubmenu.MenuItems.Add(submenuMenuItem);
    fileMenuItem.MenuItems.Add("-"); // add a separator
    fileMenuItem.MenuItems.Add(fileExitMenuItem);


    toolBar.Appearance = ToolBarAppearance.Normal;
    //toolBar.Appearance = ToolBarAppearance.Flat;
    toolBar.ImageList = imageList;
    toolBar.ButtonSize = new Size(14, 6);    

    separatorToolBarButton.Style = ToolBarButtonStyle.Separator;
    newToolBarButton.ToolTipText = "New Document"; 
    newToolBarButton.ImageIndex = 0;
    openToolBarButton.ToolTipText = "Open Document"; 
    openToolBarButton.ImageIndex = 1;
    saveToolBarButton.ToolTipText = "Save"; 
    saveToolBarButton.ImageIndex = 2;
    printToolBarButton.ToolTipText = "Print"; 
    printToolBarButton.ImageIndex = 3;

    toolBar.ButtonClick += new ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);

    toolBar.Buttons.Add(separatorToolBarButton);
    toolBar.Buttons.Add(newToolBarButton);
    toolBar.Buttons.Add(openToolBarButton);
    toolBar.Buttons.Add(saveToolBarButton);
    toolBar.Buttons.Add(separatorToolBarButton);
    toolBar.Buttons.Add(printToolBarButton);

    tree.Top = 40;
    tree.Left = 20;
    tree.Width = 100;
    tree.Height = 100;

    label.Location = new Point(220, 40);
    label.Size = new Size(160, 30);
    label.Text = "Yes, click the button";
    
    button.Location = new Point(220, 80);
    button.Size = new Size(100, 30);
    button.Text = "Click this";
    button.Click += new System.EventHandler(this.button_Click);

    statusBarPanel1.BorderStyle = StatusBarPanelBorderStyle.Sunken;
    statusBarPanel1.Text = "Press F1 for Help";
    statusBarPanel1.AutoSize = StatusBarPanelAutoSize.Spring;
    statusBarPanel2.BorderStyle = StatusBarPanelBorderStyle.Raised;
    statusBarPanel2.ToolTipText = System.DateTime.Now.ToShortTimeString();
    statusBarPanel2.Text = System.DateTime.Today.ToLongDateString();
    statusBarPanel2.AutoSize = StatusBarPanelAutoSize.Contents;
    statusBar.ShowPanels = true;
    statusBar.Panels.Add(statusBarPanel1);
    statusBar.Panels.Add(statusBarPanel2);


    this.Menu = mainMenu;
    this.Controls.Add(toolBar);
    this.Controls.Add(tree);
    this.Controls.Add(label);
    this.Controls.Add(button);
    this.Controls.Add(statusBar);
  }


  // -------------- Event Handlers --------------------------

  private void fileNewMenuItem_Click(Object sender, EventArgs e) {
    MessageBox.Show("You clicked the File -- New menu.", "The Event Information");
  }

  private void fileOpenMenuItem_Click(Object sender, EventArgs e) {
    MessageBox.Show("You clicked the File -- Open menu.", "The Event Information");
  }

  private void fileSaveMenuItem_Click(Object sender, EventArgs e) {
    MessageBox.Show("You clicked the File -- Save menu.", "The Event Information");
  }

  private void fileSaveAsMenuItem_Click(Object sender, EventArgs e) {
    MessageBox.Show("You clicked the File -- Save As menu.", "The Event Information");
  }

  private void fileExitMenuItem_Click(Object sender, EventArgs e) {
    MessageBox.Show("You clicked the File -- Exit As menu.", "The Event Information");
  }

  private void submenuMenuItem_Click(Object sender, EventArgs e) {
    MessageBox.Show("You clicked the submenu.", "The Event Information");
  }

  protected void toolBar_ButtonClick(Object sender, ToolBarButtonClickEventArgs e) {
    
    // Evaluate the Button property to determine which button was clicked.
    switch (toolBar.Buttons.IndexOf(e.Button)) {
      case 1:
        MessageBox.Show("Second button.", "The Event Information");
        break;
      case 2:
        MessageBox.Show("third button", "The Event Information");
        break;
      case 3:
        MessageBox.Show("fourth button.", "The Event Information");
        break;
    }
  }        

  protected override void OnClosing(CancelEventArgs e) {
    MessageBox.Show("Exit now.", "The Event Information");
  }

  private void button_Click(Object sender, System.EventArgs e) {
    MessageBox.Show("Thank you.", "The Event Information");
  }

  // -------------- End of Event Handlers -------------------



  public static void Main() {
    MyWinApp form = new MyWinApp();
    Application.Run(form);
  }

}

Copyright © 2009 O'Reilly Media, Inc.