Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse dropdowns in a toolbar in VB .NET
Keywordstoolbar, ComboBox, VB .NET
CategoriesControls, VB.NET
 
This example shows how to implement three different styles of dropdown list in a VB .NET toolbar.

The first Dropdown style displays a simple list of text items. Create a ContextMenu control and give it some menu items.

Create the Toolbar. Open the Properties window, select the Buttons property, and click the ellipsis to the right. Create a button, set its Style property to DropDownButton and its DropDownMenu property to the context menu you created. Set the button's Text property to whatever text you want it to initially display.

Add code to the context menu's menu items' event handlers. In particular, you may want those commands to set the dropdown button's text to the menu item's text.

This example lets you select some text, a line width, and a color. It then dras a box containing the text using the color and line width. The Form1_Paint event handler draws the rectangle and text.

You select the text using a textual dropdown menu on the toolbar. The menu items mnuTextApple, mnuTextBanana, and so on handle these selections. In this program, those event handlers call subroutine SelectText.

Subroutine SelectText saves the selected text in the variable m_SelectedText, updates the dropdown button's text, and calls Refresh to make the Paint event handler redraw the form.

 
Private m_SelectedText As String

' Draw a box with some text.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    Dim gr As Graphics = Me.CreateGraphics()
    gr.DrawRectangle( _
        New Pen(m_SelectedColor, m_SelectedLineWidth), _
        10, tbrButtons.Height + 10, ClientSize.Width - 20, _
            ClientSize.Height - tbrButtons.Height - 20)

    Dim new_font As New Font("Times New Roman", 20, _
        FontStyle.Bold, GraphicsUnit.Point)
    gr.DrawString(m_SelectedText, new_font, _
        New SolidBrush(m_SelectedColor), _
        20, tbrButtons.Height + 20)
End Sub

Private Sub mnuTextApple_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    mnuTextApple.Click
    SelectText("Apple")
End Sub
Private Sub mnuTextBanana_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    mnuTextBanana.Click
    SelectText("Banana")
End Sub
Private Sub mnuTextGiraffe_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    mnuTextGiraffe.Click
    SelectText("Giraffe")
End Sub
Private Sub mnuTextMaple_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    mnuTextMaple.Click
    SelectText("Maple")
End Sub

Private Sub SelectText(ByVal text As String)
    m_SelectedText = text
    cboText.Text = text

    ' Redraw.
    Refresh()
End Sub
 
The second kind of dropdown uses text but a single set of event handlers to handle all of the context menu's menu items.

Create a new context menu and a new dropdown button on the toolbar, and associate the menu with the button as before.

In the Form's Load event handler, use AddHandler to set event handlers for each of the context menu's items' Click events.

 
' Use one event handler to select all line widths.
AddHandler mnuLineWidth1.Click, AddressOf _
    LineWidthMenu_Click
AddHandler mnuLineWidth2.Click, AddressOf _
    LineWidthMenu_Click
AddHandler mnuLineWidth3.Click, AddressOf _
    LineWidthMenu_Click
AddHandler mnuLineWidth4.Click, AddressOf _
    LineWidthMenu_Click
AddHandler mnuLineWidth5.Click, AddressOf _
    LineWidthMenu_Click
 
The event handler routine LineWidthMenu_Click gets a reference to the menu item that triggered the event. It passes subroutine SelectLineWidth the menu item's Text.

Subroutine SelectLineWidth saves the new width in the m_SelectedLineWidth variable, updates the text displayed on the dropdown button, and calls Refresh to redraw the form.

 
' The user selected a line width.
Private Sub LineWidthMenu_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    mnuLineWidth1.Click
    Dim menu_item As MenuItem = CType(sender, MenuItem)
    SelectLineWidth(Integer.Parse(menu_item.Text))
End Sub

' Select this line width.
Private Sub SelectLineWidth(ByVal line_width As Integer)
    ' Save the line width for drawing.
    m_SelectedLineWidth = line_width

    ' Display the selected line width.
    cboLineWidth.Text = line_width.ToString

    ' Redraw.
    Refresh()
End Sub
 
The third type of dropdown uses bitmaps. Create a context menu and toolbar dropdown button as before.

In the context menu, set each menu item's OwnerDraw property to True.

Make an ImageList control and fill it with the images you want to display on the dropdown button. This example displays 16x16 pixel patches of color to represent the selected color. Set the toolbar's ImageList property to this ImageList and set the dropdown button's ImageIndex property to the image you want to display initially.

Because the context menu's items have OwnerDraw set to True, the program needs to respond to two events for each button: MeasureItem and DrawItem. This example uses the same two subroutines for all of the menu items. In the Form's Load event handler, it uses AddHandler to set these event handlers.

 
AddHandler mnuBlack.MeasureItem, AddressOf _
    ColorMenu_MeasureItem
AddHandler mnuBlack.DrawItem, AddressOf ColorMenu_DrawItem
AddHandler mnuWhite.MeasureItem, AddressOf _
    ColorMenu_MeasureItem
AddHandler mnuWhite.DrawItem, AddressOf ColorMenu_DrawItem
AddHandler mnuRed.MeasureItem, AddressOf _
    ColorMenu_MeasureItem
AddHandler mnuRed.DrawItem, AddressOf ColorMenu_DrawItem
AddHandler mnuGreen.MeasureItem, AddressOf _
    ColorMenu_MeasureItem
AddHandler mnuGreen.DrawItem, AddressOf ColorMenu_DrawItem
AddHandler mnuBlue.MeasureItem, AddressOf _
    ColorMenu_MeasureItem
AddHandler mnuBlue.DrawItem, AddressOf ColorMenu_DrawItem
 
Subroutine ColorMenu_MeasureItem sets the menu item's width and height to 16, the size of the images we will use. Subroutine ColorMenu_DrawItem fills a menu item with the currently selected color. Between them, these two routines are responsible for drawing the context menu's menu items.
 
' Return the size needed for a color 
' dropdown item.
Private Sub ColorMenu_MeasureItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
    Handles mnuBlack.MeasureItem
    e.ItemWidth = 16
    e.ItemHeight = 16
End Sub

' Draw a color dropdown item.
Private Sub ColorMenu_DrawItem(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DrawItemEventArgs) _
    Handles mnuBlack.DrawItem
    Dim menu_item As MenuItem = CType(sender, MenuItem)
    e.Graphics.FillRectangle( _
        New SolidBrush(Color.FromName(menu_item.Text)), _
        e.Bounds)
End Sub
 
The context menu's item event handlers each call subroutine SelectColor. Subroutine SelectColor saves the selected color in the variable m_SelectedColor, sets the dropdown button's ImageIndex property so it displays the correct color, and redraws the form.
 
' The user selected a color.
Private Sub mnuBlack_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuBlack.Click
    SelectColor(Color.Black, ColorIndex.Black)
End Sub
Private Sub mnuWhite_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuWhite.Click
    SelectColor(Color.White, ColorIndex.White)
End Sub
Private Sub mnuRed_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuRed.Click
    SelectColor(Color.Red, ColorIndex.Red)
End Sub
Private Sub mnuGreen_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuGreen.Click
    SelectColor(Color.Green, ColorIndex.Green)
End Sub
Private Sub mnuBlue_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuBlue.Click
    SelectColor(Color.Blue, ColorIndex.Blue)
End Sub

' Select this color.
Private Sub SelectColor(ByVal color_value As Color, ByVal _
    color_index As ColorIndex)
    ' Save the color for drawing.
    m_SelectedColor = color_value

    ' Display the proper color image.
    cboColor.ImageIndex = color_index

    ' Redraw.
    Refresh()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated