ContextMenuStrip隐藏和取消隐藏按钮

时间:2013-11-07 05:51:37

标签: c# winforms .net-4.5

我正在为一个项目开发系统托盘应用程序。当我点击“Toggle On”时,我现在仍然坚持它不会改变为“Toggle Off”,我希望也许有人可以告诉我我做错了什么并帮助我修复它。

上下文菜单的代码

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

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

1 个答案:

答案 0 :(得分:0)

您只需要toggle on/off的1个菜单项:

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

请注意,切换开/关项需要一些初始化状态,假设它是 off 状态(这意味着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;
}