3

I have a C# winForm project that uses a ContextMenuStrip. I dynamically add ToolStripMenuItems to the ContextMenuStrip based on use interaction. When I add a new ToolStripMenuItem I set it's Text property and Image property. I don't know how to the set the Image property without getting the image from the location where it's at. How do I add the imagine to my project? Here's an example of what my code is doing

    ContextMenuStrip cxtMnuStrp = new ContextMenuStrip;

    private void Button_Click(object sender, EventArgs e)
    {
       // some filtering and logic
       // to determine weather to 
       // create and add a ToolStripMenuItem
       // blah, blah, blah...

       ToolStripMenuItem item = new ToolStripMenuItem("uniqueName");

       item.Image = Image.FromFile(@"C:\MyFolder\MyIcon.ico");

       if (cxtMnuStrp.Items.ContainsKey(item) == false)
           cxtMnuStrp.Items.Add(item);
    }

With "item.Image = Image.FromFile(@"C:\MyFolder\MyIcon.ico")" When I distribute my each machine would have to have the "C:\MyFoler" directory and also have the "MyIcon.ico" on their computer in the "C:\MyFoler" directory.

Plus it doesn't seem right that I have hit the hard drive each time I want to add an icon to my ToolStripMenuItem

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Max Eisenhardt
  • 442
  • 2
  • 9
  • 20

1 Answers1

8

You could save your icons in a resource file or save the image as a embedded resource.

Using the resource file.

Adding the images as a embedded resource

You code will will be as shown below.

private void BuildContextMenuStrip_Click(object sender, EventArgs e)
{
    ContextMenuStrip cxtMnuStrp = new ContextMenuStrip();

    ToolStripMenuItem item = new ToolStripMenuItem("uniqueName") { Image = WindowsFormsApplication2.Properties.Resources.Search.ToBitmap() };

    if (cxtMnuStrp.Items.Contains(item) == false)
        cxtMnuStrp.Items.Add(item);

    this.ContextMenuStrip = cxtMnuStrp;
}

Note:

  1. If you added a icon to the resource file. You will have to convert it to a image by using .ToBitmap().
  2. The images are now available in intellisense instead of using path strings.
  3. I have added the contextMenuStrip to the form in the example above.

In addition to the information provided on how to add resources in the links above you can add them as follows

enter image description here

Community
  • 1
  • 1
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • Hey Romulus I have another question...how do I make the icon clickable? – Max Eisenhardt Apr 25 '13 at 01:14
  • The ToolStripMenuItem acts as a container in which you can add either icon or a text caption or both or neither. When you mouseover, it is the container that receives focus as a whole and not only the image or the text. Similarly, when you click it is the ToolStripMenuItem.Click event that gets called. You need to handle that event. – Patrick D'Souza Apr 25 '13 at 02:15
  • Ok thx. I understand that the ToolStripMenuItem has a click event but I only want to fire the click event on the Image click not the Text. How do I go about this? – Max Eisenhardt Apr 25 '13 at 11:09