on click event for item on a dropdown button control on toolbar

C

Curtis

Somehow I'm just missing something.

I don't know how to catch when a user clicks on an item in a dropdown list
for a dropdown style button on a toolbar.

I create a new app. Form1. Add a toolbar. Add a button and make it's
style dropdown. In the code I create a contextmenu and add three items.

This is all fine. But when I click on one of the menu items I can't figure
out how to know it's been clicked and what menu item it is.

Here's the test code:

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar

Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.ToolBar1 = New System.Windows.Forms.ToolBar

Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton

Me.SuspendLayout()

'

'ToolBar1

'

Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton()
{Me.ToolBarButton1})

Me.ToolBar1.DropDownArrows = True

Me.ToolBar1.Location = New System.Drawing.Point(0, 0)

Me.ToolBar1.Name = "ToolBar1"

Me.ToolBar1.ShowToolTips = True

Me.ToolBar1.Size = New System.Drawing.Size(456, 42)

Me.ToolBar1.TabIndex = 0

'

'ToolBarButton1

'

Me.ToolBarButton1.Style =
System.Windows.Forms.ToolBarButtonStyle.DropDownButton

Me.ToolBarButton1.Text = "test"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(456, 334)

Me.Controls.Add(Me.ToolBar1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

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

Dim cm As New ContextMenu

cm.MenuItems.Add("one")

cm.MenuItems.Add("two")

ToolBar1.Buttons(0).DropDownMenu = cm

End Sub

End Class
 
M

Mohamoss

Hi Curtis
What you should be handing is the MenuItem click event , you could create
a separate handler that handles the click on each of the menu items of
your context menu , your handler should be something like that
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MenuItem1.Click
MessageBox.Show("Source Control: " & CType(sender,
MenuItem).Parent.GetContextMenu().SourceControl.ToString)
'If ContextMenu1.SourceControl Is Nothing Then
'MessageBox.Show("Yes")
'Else
' MessageBox.Show(ContextMenu1.SourceControl.Name)
'End If

End Sub
in this example you see you can get also the source control ( if you are
using the menu with more than one control) . you would know under with
control this item was clicked
Hope this helps

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top