3

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
salar_cpp_cs
  • 43
  • 2
  • 6

4 Answers4

1

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

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);
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
1

Try this:

myContextMenuStrip.ShowImageMargin = true;
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Daniel Schealler
  • 390
  • 1
  • 16
0

Image Property DisplayStyle must set to ImageAndText Or Image

Saleh
  • 2,982
  • 5
  • 34
  • 59
  • i don't want to have only images , i want both text items and left of them images ! if my friends explain with code i will be very thankfull . – salar_cpp_cs May 16 '11 at 15:37