如何找到上下文菜单已显示而不处理点击事件

发布于 2024-11-28 10:46:53 字数 415 浏览 1 评论 0原文

我有一个简单的问题,

我正在开发一个自定义图表控件。图表控件允许用户绘制线条或放置矩形(可以是任何东西)。此开始绘制/放置选项设置为控制鼠标按下、左键单击和鼠标按下右键单击设置为显示上下文菜单。

我的问题是,如果用户决定不选择任何内容并在显示时从上下文菜单中退出,他们应该左键单击控件,因为右键单击将再次显示菜单。但目前上下文菜单正在更换,左键单击的其余部分也被执行。如何验证左键单击时显示上下文菜单,并且用户希望从上下文菜单中退出而不是执行开始绘制/放置逻辑。

其他信息: 我正在开发 C# 项目,我尝试捕获上下文菜单关闭事件,但它会在控制鼠标按下事件之前触发并自行关闭,并且我无法验证之前是否显示了上下文菜单,以避免经历鼠标左键的其余部分向下事件逻辑。

提前非常感谢任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2024-12-05 10:46:54

这是一个简单的解决方案。

假设窗体上有一个 ListBox 控件,该控件具有与其关联的 ContextMenu。现在,我们希望在每次单击控件时向控件添加一个列表项:

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

现在在表单级别定义一个名为 menuClosed 的 bool 变量,如下所示:

private bool menuClosed = false;

现在捕获上下文菜单的 Closed > 像这样的事件并翻转标志:

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

现在更新将项目添加到列表框控件的代码,如下所示:

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

我只是在上下文菜单关闭时将 bool 变量设置为 true,然后检查 bool 标志看看某个项目是否应该添加到列表中。您可以使用这种相同的机制来确定是否应该执行特定命令。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文