AddThis Social Bookmark Button

Print

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

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

Pages: 1, 2, 3, 4

Next Pagearrow