简体   繁体   English

单击自定义 Zedgraph ToolStripMenuItem 时不会检查

[英]Custom Zedgraph ToolStripMenuItem won't check when clicked

I customize the right click menu thanks to this :由于这个,我自定义了右键菜单:

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

And

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

But the menu Simple Cursor won't check when clicked.但是单击时不会检查菜单Simple Cursor I try to force the sender in the function DisplaySimpleCursor() , it doesn't work.我尝试在函数DisplaySimpleCursor()强制发送者,它不起作用。

When I debug my app, I see that in DisplaySimpleCursor() , the sender's property Checked is set to true.当我调试我的应用程序时,我看到在DisplaySimpleCursor() ,发件人的属性Checked设置为 true。

What am I missing ?我错过了什么?

As the menu is build on the heat, the checkOnClick means nothing since the object is destroyed (I guess) everytime the menu is hidden.由于菜单是建立在热量上的, checkOnClick没有任何意义,因为每次隐藏菜单时对象都会被破坏(我猜)。

The solution was to set the property :解决方案是设置属性:

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

Try this.尝试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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