开发者

How to find Context menu was shown and not to handle click events

开发者 https://www.devze.com 2023-03-26 22:31 出处:网络
I got a quick question, I am developing a custom chart control. The chart control let users to draw lines or drop rectangle (could be anything). This start draw/ drop options are set to controls mous

I got a quick question,

I am developing a custom chart control. The chart control let users to draw lines or drop rectangle (could be anything). This start draw/ drop options are set to controls mouse down, left button click and mouse down right click is set to show context menu.

My problem is, if user decide not to select anything and come out from context menu while it is shown they should left click on control as right click will show the menu again. But at the moment context menu is diapering and rest of the left click also executed. How can I verify context menu was shown on left click and user wants to come out from context menu and not to execute start draw/ drop logic.

Additional Info: I am 开发者_如何转开发working on C# project, I tried to capture context menu closed event but it triggers and closes itself before it comes to controls mouse down event and I can not verify was context menu was shown before to avoid going through rest of the left mouse button down event logic.

Any help is greatly appreciated in advance.


Here's a simple solution.

Suppose you have a ListBox control on your form which has a ContextMenu associated with it. Now we want to add a list item to the control each time it is clicked:

    private void listBox1_MouseClick(object sender, MouseEventArgs e)
    {
        listBox1.Items.Add("new item added - " + DateTime.Now.ToLongTimeString());
    }

Now define a bool variable at your form level called menuClosed like so:

private bool menuClosed = false;

Now capture the context menu's Closed event like so and flip the flag:

    private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        menuClosed = true;
    }

Now update the code that adds an item to the list box control like the following:

    private void listBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (!menuClosed)
            listBox1.Items.Add("new item added - " + DateTime.Now.ToLongTimeString());
    }

I'm just setting the bool variable to true when the context menu is closed, then i check for the bool flag to see if an item should be added to the list. you can use this kind of same mechanism to determine a specific command should be executed or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消