DropDownButton without toolbar

  • Thread starter Thread starter Fredrik Melin
  • Start date Start date
F

Fredrik Melin

Have anyone seen a component or know how to create a button like toolbar
dropdown button without the toolbar (to be used like a normal button)

Regards
Fredrik Melin
 
Fredrik,
Not sure of a pre-built control, however I've used the Click event of a
normal Button to call ContextMenu.Show method.

Something like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim pos As Point = Button1.Location
pos.Y += Button1.Height
Me.ContextMenu1.Show(Me, pos)
End Sub

Hope this helps
Jay
 
Yes, but I wanted the nice feature with a arrow down for options and to have
a "default" click event, if no menu was selected.
 
Fredrik,
I would use either two buttons...

Or I would create a custom control (possibly deriving from Button) & handle
painting & mouse clicks (possibly overriding Control.OnClick) myself to
decide if the "default" click event should be handled or the ContextMenu
should be displayed...

Post if you want a sample, I can see if I can come up with one later, no
promises...

Hope this helps
Jay
 
Did a User Control with a little properties, achor of buttons and so on,
that worked nice.
Set ContextMenu & ButtonText as properties, subscribe on CommandButtonClick
event to get the button click event.

Thanks Jay for ideas.

Code below for others to use if anyone need it.

Regards
Fredrik Melin

'-------------------------------------------------------------------

Public Class ucDropDownButton
Inherits System.Windows.Forms.UserControl

#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

'UserControl 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 cmdCommand As System.Windows.Forms.Button
Friend WithEvents cmdDropDown As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.cmdCommand = New System.Windows.Forms.Button
Me.cmdDropDown = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'cmdCommand
'
Me.cmdCommand.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.cmdCommand.Location = New System.Drawing.Point(5, 4)
Me.cmdCommand.Name = "cmdCommand"
Me.cmdCommand.Size = New System.Drawing.Size(97, 24)
Me.cmdCommand.TabIndex = 0
Me.cmdCommand.Text = "Create"
'
'cmdDropDown
'
Me.cmdDropDown.Anchor =
CType(((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.cmdDropDown.Location = New System.Drawing.Point(101, 4)
Me.cmdDropDown.Name = "cmdDropDown"
Me.cmdDropDown.Size = New System.Drawing.Size(18, 24)
Me.cmdDropDown.TabIndex = 1
Me.cmdDropDown.Text = "V"
'
'ucDropDownButton
'
Me.Controls.Add(Me.cmdDropDown)
Me.Controls.Add(Me.cmdCommand)
Me.Name = "ucDropDownButton"
Me.Size = New System.Drawing.Size(122, 31)
Me.ResumeLayout(False)

End Sub

#End Region
Public Event CommandButtonClicked(ByVal sender As Object, ByVal e As
EventArgs)

Private m_cMenu As ContextMenu

Public Property ButtonText() As String
Get
Return cmdCommand.Text
End Get
Set(ByVal Value As String)
cmdCommand.Text = Value
End Set
End Property

Public Property cMenu() As ContextMenu
Set(ByVal Value As ContextMenu)
m_cMenu = Value
End Set
Get
Return m_cMenu
End Get
End Property

Private Sub cmdDropDown_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDropDown.Click
Dim pos As Point = cmdDropDown.Location
pos.Y += cmdDropDown.Height
Me.m_cMenu.Show(Me, pos)
End Sub


Private Sub ucDropDownButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdCommand.Click
RaiseEvent CommandButtonClicked(sender, e)
End Sub
End Class
 

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

Back
Top