Toolbar In C#

Toolbar in C#

Note: The Toolbar control is not available in Toolbox of Visual Studio 2010 or later versions. The ToolStrip control replaces Toolbar in Visual Studio 2010 or later versions. Please refer to ToolStrip Control In Visual Studio to get started with the ToolStrip control.  

In this article, I will discuss how to create and use a Toolbar using Toolbar class in a Windows Forms application. A Toolbar control is a combination of Toolbar buttons where each button represents a function. A Toolbar button can display an image, text or a combination of both. The button click event handler is responsible for executing some code.

If you are using previous versions of Visual Studio, read my article Tutorial: Working with Toolbars in C#.

Creating a Toolbar

Toolbar class represents a Toolbar.

  1. Toolbar mainToolBar = new ToolBar();  
Once an object is created, the next step is to set its properties. The following code snippet sets some properties of a Toolbar.

  1. mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;  
  2. mainToolBar.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;  
  3. mainToolBar.Divider = true;  
  4. mainToolBar.DropDownArrows = true;  
  5. mainToolBar.ShowToolTips = true;  
  6. mainToolBar.Size = new System.Drawing.Size(400, 25);  
  7. mainToolBar.TabIndex = 0;  
  8. mainToolBar.Wrappable = false;  

A Toolbar is a combination of Toolbar buttons. ToolBarButton class represents a Toolbar button. The following code snippet creates five buttons and add them to Toolbar.

  1. ToolBarButton toolBarButton1 = newToolBarButton();  
  2. ToolBarButton toolBarButton2 = newToolBarButton();  
  3. ToolBarButton toolBarButton3 = newToolBarButton();  
  4. ToolBarButton toolBarButton4 = newToolBarButton();  
  5. ToolBarButton toolBarButton5 = newToolBarButton();  
  6. toolBarButton1.Text = "New";  
  7. toolBarButton2.Text = "Open";  
  8. toolBarButton3.Text = "Save";  
  9. toolBarButton4.Text = "Print";  
  10. toolBarButton5.Text = "Exit";  
  11. mainToolBar.Buttons.Add(toolBarButton1);  
  12. mainToolBar.Buttons.Add(toolBarButton2);  
  13. mainToolBar.Buttons.Add(toolBarButton3);  
  14. mainToolBar.Buttons.Add(toolBarButton4);  
  15. mainToolBar.Buttons.Add(toolBarButton5);  

Now, let's add a Toolbar button click event handler. This handler code will be executed when a button on the Toolbar is clicked.

  1. mainToolBar.ButtonClick += newToolBarButtonClickEventHandler(  
  2. mainToolBarClicked);  

In the end, we add Toolbar to the Form.

  1. Controls.Add(mainToolBar);  

The following code snippet sets these properties and adds three images to the Toolbar control and later loops through the images and displays them on a Form.

  1. protected ToolBar mainToolBar;    
  2. public void CreateDnamicToolbar() {    
  3.     mainToolBar = new ToolBar();    
  4.     mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;    
  5.     mainToolBar.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;    
  6.     mainToolBar.Divider = true;    
  7.     mainToolBar.DropDownArrows = true;    
  8.     mainToolBar.ShowToolTips = true;    
  9.     mainToolBar.Size = new System.Drawing.Size(400, 25);    
  10.     mainToolBar.TabIndex = 0;    
  11.     mainToolBar.Wrappable = false;    
  12.     mainToolBar.ButtonClick += new ToolBarButtonClickEventHandler(mainToolBarClicked);    
  13.     ToolBarButton toolBarButton1 = new ToolBarButton();    
  14.     ToolBarButton toolBarButton2 = new ToolBarButton();    
  15.     ToolBarButton toolBarButton3 = new ToolBarButton();    
  16.     ToolBarButton toolBarButton4 = new ToolBarButton();    
  17.     ToolBarButton toolBarButton5 = new ToolBarButton();    
  18.     toolBarButton1.Text = "New";    
  19.     toolBarButton2.Text = "Open";    
  20.     toolBarButton3.Text = "Save";    
  21.     toolBarButton4.Text = "Print";    
  22.     toolBarButton5.Text = "Exit";    
  23.     mainToolBar.Buttons.Add(toolBarButton1);    
  24.     mainToolBar.Buttons.Add(toolBarButton2);    
  25.     mainToolBar.Buttons.Add(toolBarButton3);    
  26.     mainToolBar.Buttons.Add(toolBarButton4);    
  27.     mainToolBar.Buttons.Add(toolBarButton5);    
  28.     Controls.Add(mainToolBar);    
  29. }    
  30. private void mainToolBarClicked(Object sender, ToolBarButtonClickEventArgs e) {    
  31.     switch (mainToolBar.Buttons.IndexOf(e.Button)) {    
  32.         case 0:    
  33.             MessageBox.Show("Add New file code here");    
  34.             break;    
  35.         case 1:    
  36.             OpenFileDialog openDlg = new OpenFileDialog();    
  37.             if (DialogResult.OK == openDlg.ShowDialog()) {    
  38.                 string fileName = openDlg.FileName;    
  39.                 MessageBox.Show(fileName);    
  40.             }    
  41.             break;    
  42.         case 2:    
  43.             SaveFileDialog saveDlg = new SaveFileDialog();    
  44.             if (DialogResult.OK == saveDlg.ShowDialog()) {    
  45.                 string fileName = saveDlg.FileName;    
  46.                 MessageBox.Show(fileName);    
  47.             }    
  48.             break;    
  49.         case 3:    
  50.             PrintDialog printDlg = new PrintDialog();    
  51.             printDlg.ShowDialog();    
  52.             break;    
  53.         case 4:    
  54.             this.Close();    
  55.             break;    
  56.     }    
  57. }    
Summary

In this article, we discussed how to create and use a Toolbar control in a Windows Forms application.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.