Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add a heading for the context menu.
The heading to be not clickable, with gray back ground with bold text.
I tried by searching and using ownerdraw and drawitem but not succeeded.
when ownerdraw of menuitem set to true and drawitem is used for drawing the contextmenu is not drawing the menuitem.

I want like this

Heading (in bold with gray background and not clickable)
MenuItem1
MenuItem2
MenuItem3

Can anyone please help.

Thanks
Posted
Updated 1-Jun-12 14:34pm
v2

1 solution

I don't think you can do it in the designer but I did come across a way to do it programmatically:

http://stackoverflow.com/questions/4595116/c-sharp-context-menu[^]

The relevant code from this article (again,not mine but from this article so this is a quote from Hans Passant):

SQL
contextMenuStrip1.Items.Insert(0, new ToolStripLabel("Please select an option"));
contextMenuStrip1.Items.Insert(1, new ToolStripSeparator());
 
Share this answer
 
Comments
Csharp_Learner 1-Jun-12 23:37pm    
Thank you very much.
Earlier I used ContextMenu which has MenuItems property which allowed only MenuItem object.
With your solution I used ContextMenuStrip which allowed ToolStripLabel. I set ToolStripLabel1.BackColor = Color.DarkBlue;
It did not work. So I used Paint event of ToolStripLabel
var heading = new ToolStripLabel("--Select an Option--");
heading.Paint += heading_Paint;
ContextMenuStrip.Items.Add(heading);
void heading_Paint(object sender, PaintEventArgs e) {
e.Graphics.FillRectangle(Brushes.DarkBlue, e.ClipRectangle);
e.Graphics.DrawString((sender as ToolStripLabel).Text, new Font("Arial", 8, FontStyle.Bold), Brushes.White, e.ClipRectangle);
}

Then it worked.

Thank you again for the help :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900