简体   繁体   中英

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. I try to force the sender in the function DisplaySimpleCursor() , it doesn't work.

When I debug my app, I see that in DisplaySimpleCursor() , the sender's property Checked is set to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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