简体   繁体   中英

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);

        }

Use something like this:

string[] readText = File.ReadAllLines(@"Path\item.txt");
foreach (string txt in readText)
{
    string i = txt.Split(new string[] { "|" }, StringSplitOptions.None)[0];

    ToolStripItem subItem = new ToolStripMenuItem(i);
    var iconImage = new Bitmap(i[1].Replace("/", @"\"));
    subItem.Image = iconImage;

    nToolStripMenuItem.DropDownItems.Add(subItem);
}

使用Bitmap.FromFile

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM