ARTICLE

Toolbar in VB.NET

Posted by Mahesh Chand Articles | Windows Controls July 18, 2010
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.
Download Files:
 
Reader Level:

Toolbar in VB.NET

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.

Login to add your contents and source code to this article
share this article :
post comment
 

ToolBarButton FlyStyle

Posted by hou jun Jan 06, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor