Search code examples
c#menustrip

How to set menustrip icon from txt file C#


I have a txt file and this is the content :

Itemname|path/to/my/icon.png

I used this code:

foreach (var txt in readText)
{
     string i = txt.Split(new string[] { "|" }, StringSplitOptions.None)[0];
     ToolStripItem subItem = new ToolStripMenuItem(i);
     nToolStripMenuItem.DropDownItems.Add(subItem);

}
string[] readText = File.ReadAllLines(@"Path\item.txt");

I have success in adding the item to the menustrip but how can I set the icon for those item.

string icon = txt.Split(new string[] { "|" }, StringSplitOptions.None)[1];

I have came up with a solution:

foreach (var txt in readText)
        {
            string i = txt.Split(new string[] { "|" }, StringSplitOptions.None)[0];
            ToolStripItem subItem = new ToolStripMenuItem(i);
            subItem.Image = Bitmap.FromFile(txt.Split(new string[] { "|" }, StringSplitOptions.None)[1]);
            nToolStripMenuItem.DropDownItems.Add(subItem);

        }

Solution

  • Use Bitmap.FromFile

    subItem.Image = Bitmap.FromFile("filepath");