Adding a "Title" to a context menu

  • Thread starter Thread starter JB
  • Start date Start date
J

JB

Hi All,

I wonder if there's a way to add some sort of title to a context menu.
A bit like message box titles for instance.

Thanks
JB
 
I haven't tried this, but maybe you can get something from a new, specially
formatted MenuItem at the top of your menu. Maybe an owner drawn MenuItem
with the title displayed in sufficient contrast to the real MenuItems. Or,
maybe you can put the title text in an image and use MenuItem.ImageUrl to
display it.

If you have the time and energy to investigate, please post back your results.
 
Not that I'm aware of, or that is readily apparent from looking at the
ContextMenuStrip class members in MSDN.

http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip_members(v=VS.100).aspx


That said, it's an interesting question, and I look forward to being
corrected :-)

I cannot remember where I found this on the web (apologies to the
author) but these are the building blocks that I use to do this
particular job.

''' <summary>
''' ContextMenuStrip with a non-clickable "header" bar
''' </summary>
''' <remarks>
''' <para>
''' We needed this sub-class of
''' <see cref="T:System.Windows.Forms.ContextMenuStrip" /> because the
original automatically hides the menu when any item is clicked.
''' </para>
''' <para>
''' Given that our header item doesn't want this behaviour, we have to
not only extend the
''' <see cref="T:System.Window.Forms.ToolStripMenuitem" /> itself, but
also its containing
''' <see cref="T:System.Window.Forms.ContextMenuStrip" />.
''' </para>
''' </remarks>
Friend Class HeadedContextMenuStrip
Inherits ContextMenuStrip

Public Sub New( container as IContainer )
MyBase.New( container )
End Sub

Protected Overrides Sub OnItemClicked( e as
ToolStripItemClickedEventArgs )
{
If Not ( TypeOf e.ClickedItem Is ContextMenuHeader ) Then
MyBase.OnItemClicked( e )
End If
End Property

End Class

''' <summary>
''' Header item to be used in conjunction with the HeadedContextMenuStrip.
''' </summary>
Friend Class ContextMenuHeader
Inherits ToolStripMenuItem

Private Sub New()
MyBase.New()
End Sub

Public Sub New( text as String )
MyBase,New( text )
Me.BackColor = SystemColors.GradientInactiveCaption
Me.Font = New Font(Me.Font, FontStyle.Bold)
Me.ForeColor = SystemColors.MenuText
End Sub

Protected Overrides Function CreateDefaultDropDown() as
ToolStripDropDown
ToolStripDropDownMenu menu = New ToolStripDropDownMenu()
menu.OwnerItem = Me
return menu
End Function

Protected Overrides ReadOnly Property DismissWhenClicked() as Boolean
Get
Return False
End Get
End Property

End Class

HTH,
Phill W.
 
Back
Top