How do I set an image for ContextMenuStrip items?
-
14-11-2019 - |
Question
How do I set an image for ContextMenuStrip items? I'm using C#.
Solution
You need to set the ToolStripItem.DisplayStyle Property to Image and then set the image property
Here's the sample from MSDN which
- gets the image from a file
- sets the style to Image and text
- aligns the image to MiddleLeft
- set the name of the itme
- sets the text align to MiddleRight
- sets the text
- and adds an Click event handler
Sample
this.toolStripButton1.Image = Bitmap.FromFile("c:\\NewItem.bmp");
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
this.toolStripButton1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Text = "&New";
this.toolStripButton1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
OTHER TIPS
You're looking for the (drumroll...) Image
property!
Try this:
myContextMenuStrip.ShowImageMargin = true;
Image
Property
DisplayStyle
must set to ImageAndText
Or Image
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow