【问题标题】:Custom Zedgraph ToolStripMenuItem won't check when clicked单击时不会检查自定义 Zedgraph ToolStripMenuItem
【发布时间】:2020-03-16 15:19:51
【问题描述】:

多亏了这个,我自定义了右键菜单:

lineGraphControl1.ContextMenuBuilder += new ZedGraphControl.ContextMenuBuilderEventHandler(MyContextMenuBuilder);

private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
{
    // create a new menu item
    ToolStripMenuItem item = new ToolStripMenuItem();
    // This is the user-defined Tag so you can find this menu item later if necessary
    item.Name = "simple_cursor";
    // This is the text that will show up in the menu
    item.Text = "Simple Cursor";
    item.CheckOnClick = true;
    // Add a handler that will respond when that menu item is selected
    item.Click += new System.EventHandler(DisplaySimpleCursor);
    // Add the menu item to the menu
    menuStrip.Items.Add(item);
}

但菜单Simple Cursor 在点击时不会检查。我尝试在函数DisplaySimpleCursor()中强制发送者,它不起作用。

当我调试我的应用程序时,我看到在 DisplaySimpleCursor() 中,发送者的属性 Checked 设置为 true。

我错过了什么?

【问题讨论】:

    标签: c# zedgraph toolstripmenu


    【解决方案1】:

    由于菜单是建立在热量之上的,checkOnClick 没有任何意义,因为每次隐藏菜单时对象都会被破坏(我猜)。

    解决方案是设置属性:

    // showOneCursor is a bool describing my need and toggled on click
    item.Checked = showOneCursor; 
    

    【讨论】:

      【解决方案2】:

      试试这个。

       private bool check;
          public bool Check
          {
              get { return check; }
              set { check= value; }
          }
      private void MyContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
      {
          ToolStripMenuItem item = new ToolStripMenuItem();
          item.Name = "simple_cursor";
          item.Text = "Simple Cursor";
          item.CheckOnClick = true;
              item.Checked = Check; //add this
          item.Click += new System.EventHandler(DisplaySimpleCursor);
          menuStrip.Items.Add(item);
      }
      
      
       private void DisplaySimpleCursor(object sender, EventArgs e)
              {
                  Check = false==Check;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多