0

我正在为一个项目开发系统托盘应用程序。当我单击“切换开启”时,我目前卡住了它不会自行更改为“切换关闭”,我希望也许有人能告诉我我做错了什么并帮助我修复它。

上下文菜单的代码

using System;
using System.Diagnostics;
using System.Windows.Forms;
using TestApplicationforRIVC.Properties;
using System.Drawing;

namespace TestApplicationforRIVC {

    class RIVCContextMenu {

        //Is Add Box Displayed?
        bool isAddLoaded = false;

        //Is the Clear box Displayed?
        bool isClearLoaded = false;

        //Is it listening for voice?
        bool toggleOn = false;

        //Creates this Instance
        public ContextMenuStrip Create() {
            //Add the Default Menu Options
            ContextMenuStrip menu = new ContextMenuStrip();
            ToolStripMenuItem item;
            ToolStripMenuItem off;
            ToolStripSeparator sep;

            if(toggleOn) {
                //Toggle Off
                item = new ToolStripMenuItem();
                item.Text = "Toggle Off";
                item.Click += new EventHandler(toggleOffClick);
                item.Image = Resources.toggleOff;
                menu.Items.Add(item);
            }
            if(!toggleOn) {
                //Toggle On
                item = new ToolStripMenuItem();
                item.Text = "Toggle On";
                item.Click += new EventHandler(toggleOnClick);
                item.Image = Resources.toggleOn;
                menu.Items.Add(item);
            }
            // Separator.
            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            //Add to RIVC Library
            item = new ToolStripMenuItem();
            item.Text = "Add to Library";
            item.Click += new EventHandler(addClick);
            item.Image = Resources.Add;
            menu.Items.Add(item);

            //Clear RIVC Library
            item = new ToolStripMenuItem();
            item.Text = "Clear Library";
            item.Click += new EventHandler(clearClick);
            item.Image = Resources.Delete;
            menu.Items.Add(item);

            // Separator.
            sep = new ToolStripSeparator();
            menu.Items.Add(sep);

            //Exit Program
            item = new ToolStripMenuItem();
            item.Text = "Exit";
            item.Click += new System.EventHandler(exitClick);
            item.Image = Resources.Exit;
            menu.Items.Add(item);

            return menu;
        }

        //Handles the Click Event of the Toggle On Control
        void toggleOnClick(object sender, EventArgs e) {
            toggleOn = true;
        }

        //Handles the Click Even of the Toggle Off Control
        void toggleOffClick(object sender, EventArgs e) {
            toggleOn = false;
        }

        //Handles the Click Event of the Add Control
        void addClick(object sender, EventArgs e) {
            if(!isAddLoaded) {
                isAddLoaded = true;
                //enter Dialog Box Info
                isAddLoaded = false;
            }
        }

        //Handles the Click Event of the Clear Control
        void clearClick(object sender, EventArgs e) {
            if(!isClearLoaded) {
                isClearLoaded = true;
                //enter dialog box info
                isClearLoaded = false;
            }
        }

        //Handles the Click Event of the Exit Control
        void exitClick(object sender, EventArgs e) {
            Application.Exit();
        }
    }
}

希望你们能帮助我解决这个小错误。

4

1 回答 1

0

您只需要 1 个菜单项即可toggle on/off

item = new ToolStripMenuItem();
item.Name = "onOff";//Name it to refer later if needed
item.Text = "Toggle Off";
item.Click += new EventHandler(toggleOnOffClick);
item.Image = Resources.toggleOff;
menu.Items.Add(item);

请注意,切换开/关项目需要一些初始化状态,假设它是关闭状态(这意味着的初始化值toggleOn应该是false)。这是为您准备的新toggleOnOffClick事件处理程序:

void toggleOnOffClick(object sender, EventArgs e) {
  var item = sender as ToolStripMenuItem;
  if(toggleOn){
    item.Text = "Toggle off";
    item.Image = Resources.toggleOff;
  } else {
    item.Text = "Toggle on";
    item.Image = Resources.toggleOn;
  }
  toggleOn = !toggleOn;
}
于 2013-11-07T14:02:33.570 回答