Click here to Skip to main content
15,894,405 members

How to add image to ToolStripMenuItem

d.allen101 asked:

Open original thread
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
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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