0

I wrote a windows form application which can change my GateWay automatically by executing two Batch files. Problem is that I want to show "check.ico" icon beside the item in ContextMenu list when I click on it.

Here is my code :

 public Form1()
        {
            InitializeComponent();
        }
        Image MenuStripImage = Image.FromFile(@"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\Check.ico");
        NotifyIcon TrayIcon;
        ContextMenuStrip TrayMenu;
        private void Form1_Load(object sender, EventArgs e)
        {

            Image MenuStripExit = Image.FromFile(@"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\exit.ico");
            this.WindowState = FormWindowState.Maximized;
            this.ShowInTaskbar = false;
            this.Hide();
            TrayIcon = new NotifyIcon();
            TrayMenu = new ContextMenuStrip();
            TrayMenu.Items.Add("GreenPAcket").Click += new EventHandler(GreenpacketSelect);
            //if (DLinkSelectcliced == true && GreenPacketSelected == false)
            //{
                TrayMenu.Items.Add(MenuStripImage);
            //}
            TrayMenu.Items.Add("D-Link DSL 2890AL 5GHz").Click += new EventHandler(DLinkSelect);
            //if (GreenPacketSelected == true && DLinkSelectcliced == false)
            //{
                TrayMenu.Items.Add(MenuStripImage);
            //}
            TrayMenu.Items.Add("Exit", MenuStripExit).Click += new EventHandler(Exit);
            TrayIcon.ContextMenuStrip = TrayMenu;
            TrayIcon.Visible = true;
            TrayIcon.Icon = new Icon(@"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\WindowsFormsApplication2\router.ico");

        }

        private void Exit(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

      //  bool DLinkSelectcliced = false;
        private void DLinkSelect(object sender, EventArgs e)
        {
        //    DLinkSelectcliced = true;
            Process Proc = new Process();
            Proc.StartInfo.FileName = (@"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\Dlink.lnk");
            Proc.Start();
            TrayMenu.Items.Add(MenuStripImage);
     //       GreenPacketSelected = false;
        }
      //  bool GreenPacketSelected = false;
        private void GreenpacketSelect(object sender, EventArgs e)
        {
     //       GreenPacketSelected = true;
            Process Proc = new Process();
            Proc.StartInfo.FileName = (@"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\GreenPacket.lnk");
            Proc.Start();
            TrayMenu.Items.Add(MenuStripImage);
  //          DLinkSelectcliced = false;
        }
Kusum
  • 501
  • 2
  • 11
  • 30
Hossain Ehsani
  • 385
  • 2
  • 16

1 Answers1

3

If check.ico is a check mark, you don't need it for ContextMenuItem because it has option to show check mark, just enable CheckOnClick property for that item.

But if you want to show your custom icon instead, you must first convert it to image
You can do it yourself or use this method below to let C# do it for you

Use method here to convert icon to bmp image:
http://www.dotnetfunda.com/codes/show/967/icon-to-bitmap-in-csharp-winforms
Or simply create .png/.bmp version of this icon instead.

But first you should add this icons as resources to your project because if this icons will not exist in the location you gave it won't work (so you won't be able to use it on another PC).
To do it open solution explorer, right click on project and select properties, then go to resources, select icons, click Add Resource and add all icons.

Then to load icons from resources you simply type:
notifyIcon1.Icon = Properties.Resources.iconName

To show icon as image in your ContextMenuItem method in the link like this: menu1ToolStripMenuItem.Image = ConvertFromIconToBitmap(Properties.Resources.iconName, new Size(16,16))

Btw, I glanced at your code and I think there is a lot of problems there...

Mr. Noob
  • 136
  • 1
  • 7