SIGN UP MEMBER LOGIN:    
ARTICLE

Toolbar in C#

Posted by Mahesh Chand Articles | Windows Controls C# July 18, 2010
Toolbar control is not available in Toolbox of Visual Studio 2010. ToolStrip control replaces Toolbar in Visual Studio 2010. But for backward compatibility support, Toolbar class is available in Windows Forms. In this article, I will discuss how to create and use a Toolbar using Toolbar class in a Windows Forms application.
Reader Level:
Download Files:
 

Toolbar in C#

Toolbar control is not available in Toolbox of Visual Studio 2010. ToolStrip control replaces Toolbar in Visual Studio 2010. But for backward compatibility support, Toolbar class is available in Windows Forms. 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.

Toolbar mainToolBar = new ToolBar();

 

Once an object is created, next step is to set its properties. The following code snippet sets some properties of a Toolbar.  

mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;

mainToolBar.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

mainToolBar.Divider = true;

mainToolBar.DropDownArrows = true;

mainToolBar.ShowToolTips = true;

mainToolBar.Size = new System.Drawing.Size(400, 25);

mainToolBar.TabIndex = 0;

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.

 

ToolBarButton toolBarButton1 = new ToolBarButton();

ToolBarButton toolBarButton2 = new ToolBarButton();

ToolBarButton toolBarButton3 = new ToolBarButton();

ToolBarButton toolBarButton4 = new ToolBarButton();

ToolBarButton toolBarButton5 = new ToolBarButton();

 

toolBarButton1.Text = "New";

toolBarButton2.Text = "Open";

toolBarButton3.Text = "Save";

toolBarButton4.Text = "Print";

toolBarButton5.Text = "Exit";

 

mainToolBar.Buttons.Add(toolBarButton1);

mainToolBar.Buttons.Add(toolBarButton2);

mainToolBar.Buttons.Add(toolBarButton3);

mainToolBar.Buttons.Add(toolBarButton4);

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.

mainToolBar.ButtonClick += new ToolBarButtonClickEventHandler(

    mainToolBarClicked);

 

In the end, we add Toolbar to the Form.

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.

protected ToolBar mainToolBar;

 

public void CreateDnamicToolbar()

{

    mainToolBar = new ToolBar();

    mainToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;

    mainToolBar.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

    mainToolBar.Divider = true;

    mainToolBar.DropDownArrows = true;

    mainToolBar.ShowToolTips = true;

    mainToolBar.Size = new System.Drawing.Size(400, 25);

    mainToolBar.TabIndex = 0;

    mainToolBar.Wrappable = false;

     

    mainToolBar.ButtonClick += new ToolBarButtonClickEventHandler(

        mainToolBarClicked);

 

    ToolBarButton toolBarButton1 = new ToolBarButton();

    ToolBarButton toolBarButton2 = new ToolBarButton();

    ToolBarButton toolBarButton3 = new ToolBarButton();

    ToolBarButton toolBarButton4 = new ToolBarButton();

    ToolBarButton toolBarButton5 = new ToolBarButton();

 

    toolBarButton1.Text = "New";

    toolBarButton2.Text = "Open";

    toolBarButton3.Text = "Save";

    toolBarButton4.Text = "Print";

    toolBarButton5.Text = "Exit";

 

    mainToolBar.Buttons.Add(toolBarButton1);

    mainToolBar.Buttons.Add(toolBarButton2);

    mainToolBar.Buttons.Add(toolBarButton3);

    mainToolBar.Buttons.Add(toolBarButton4);

    mainToolBar.Buttons.Add(toolBarButton5);          

 

    Controls.Add(mainToolBar);

}

 

private void mainToolBarClicked(Object sender,

                        ToolBarButtonClickEventArgs e)

{

    switch (mainToolBar.Buttons.IndexOf(e.Button))

    {

        case 0:

            MessageBox.Show("Add New file code here");

            break;

        case 1:

            OpenFileDialog openDlg = new OpenFileDialog();

            if (DialogResult.OK == openDlg.ShowDialog())

            {

                string fileName = openDlg.FileName;

                MessageBox.Show(fileName);

            }

            break;

        case 2:

            SaveFileDialog saveDlg = new SaveFileDialog();

            if (DialogResult.OK == saveDlg.ShowDialog())

            {

                string fileName = saveDlg.FileName;

                MessageBox.Show(fileName);

            }

            break;

        case 3:

            PrintDialog printDlg = new PrintDialog();

            printDlg.ShowDialog();  

            break;

        case 4:

            this.Close();  

            break;

    }

}

 

Summary

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

share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor