Toolbar Control in VB.NET

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.
  • 18231
 

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.

Protected mainToolBar As ToolBar = 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.

Dim toolBarButton1 As New ToolBarButton()

        Dim toolBarButton2 As New ToolBarButton()

        Dim toolBarButton3 As New ToolBarButton()

        Dim toolBarButton4 As New ToolBarButton()

        Dim toolBarButton5 As 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.

AddHandler mainToolBar.ButtonClick, AddressOf mainToolBarClicked

In the end, we add Toolbar to the Form.

Controls.Add(mainToolBar)

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

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        CreateDnamicToolbar()

    End Sub

    Protected mainToolBar As ToolBar

    Private Sub 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

        AddHandler mainToolBar.ButtonClick, AddressOf mainToolBarClicked

        Dim toolBarButton1 As New ToolBarButton()

        Dim toolBarButton2 As New ToolBarButton()

        Dim toolBarButton3 As New ToolBarButton()

        Dim toolBarButton4 As New ToolBarButton()

        Dim toolBarButton5 As 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)

    End Sub

    Private Sub mainToolBarClicked(ByVal sender As Object, _

   ByVal e As ToolBarButtonClickEventArgs)

        Select Case mainToolBar.Buttons.IndexOf(e.Button)

            Case 0

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

            Case 1

                Dim openDlg As New OpenFileDialog()

                If (DialogResult.OK = openDlg.ShowDialog()) Then

                    Dim fileName As String = openDlg.FileName

                    MessageBox.Show(fileName)

                End If

            Case 2

                Dim saveDlg As New SaveFileDialog()

                If (DialogResult.OK = saveDlg.ShowDialog()) Then

                    Dim fileName As String = saveDlg.FileName

                    MessageBox.Show(fileName)

                End If

            Case 3

                Dim printDlg As New PrintDialog

                printDlg.ShowDialog()

            Case 4

                Me.Close()

        End Select

    End Sub

End Class

Summary

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.