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

时间:2014-09-25 13:28:57

标签: c# zedgraph toolstripmenu

我自定义了右键菜单:

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()中看到,发件人的属性已检查设置为true。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

由于菜单是在高温下构建的,因此每次隐藏菜单时,checkOnClick都没有任何意义,因为对象被毁坏了(我猜)。

解决方案是设置属性:

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

答案 1 :(得分:0)

尝试一下。

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