Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an image for ContextMenuStrip items?

How do I set an image for ContextMenuStrip items? I'm using C#.

like image 242
salar_cpp_cs Avatar asked Sep 06 '25 06:09

salar_cpp_cs


2 Answers

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);
like image 135
Conrad Frix Avatar answered Sep 07 '25 19:09

Conrad Frix


You're looking for the (drumroll...)                                       Image property!

like image 28
SLaks Avatar answered Sep 07 '25 21:09

SLaks