c# - 如何使用按钮单击来切换 ContextMenuStrip 显示,同时允许它正常关闭(菜单项单击、失去焦点等)

标签 c# winforms user-controls contextmenustrip

我创建了一个由 Label 和 ContextMenuStrip 组成的简单 UserControl。我使它的功能类似于 ComboBox,但我显示的不是下拉列表,而是 ContextMenuStrip。

我可以正常工作,但有一些我无法弄清楚的棘手问题。

我试图使标签 ComboButton 的工作方式与 ComboBox 相同。单击按钮,出现下拉菜单。再次单击该按钮,它将缩回。问题是,单击鼠标时 ContextMenu 会消失。所以当我第二次点击按钮收回菜单时,菜单先消失,然后触发点击事件,再次显示菜单。

我仍然希望当用户选择一个菜单项时以及当他们像普通上下文菜单一样单击表单上的任意位置时菜单消失。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Windows.Forms.VisualStyles;
using System.Diagnostics;

namespace Controls
{
    public partial class CMenu : UserControl
    {
        ButtonState _buttonState = ButtonState.Normal;

        public CMenu()
        {
            InitializeComponent();
        }

        private void lblSelect_Paint(object sender, PaintEventArgs e)
        {
            base.OnPaint(e);
            ControlPaint.DrawComboButton(e.Graphics, getLabelRect(), _buttonState);
        }

        private bool IsDropdownHit(MouseEventArgs e)
        {
            Rectangle cursor = new Rectangle(e.X, e.Y, 1, 1);
            if (e.Button == MouseButtons.Left && cursor.IntersectsWith(getLabelRect()))
            {
                return true;
            }
            return false;
        }

        private void lblSelect_MouseUp(object sender, MouseEventArgs e)
        {
            if (!IsDropdownHit(e))
                return;

            if (!cmsItems.Visible)
                lblSelect.ContextMenuStrip = cmsItems;
                cmsItems.Width = lblSelect.Width;
                cmsItems.Show(lblSelect, 0, lblSelect.Height);
        }

        private Rectangle getLabelRect()
        {
            return new Rectangle(lblSelect.Width - 20, 0, 20, lblSelect.Height);
        }
    }
}

最佳答案

所以,我稍微重写了你的 mouseUp:

private void lblSelect_MouseUp(object sender, MouseEventArgs e)
    {
        if (IsDropdownHit(e) && cmsItems.Tag == null)
        {
            cmsItems.Width = lblSelect.Width;
            cmsItems.Show(lblSelect, 0, lblSelect.Height);
            cmsItems.Tag = "Shown";
        }
        else
        {
            cmsItems.Hide();
            cmsItems.Tag = null;
        }
    }

现在,它会关闭。但是 - 如果您不使用按钮关闭,则下次必须单击两次才能打开它。

此“错误”的解决方法:

void cmsItems_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        Point c = PointToClient(Cursor.Position);
        if (!IsDropdownHit(new MouseEventArgs(MouseButtons.Left, 1, c.X - lblSelect.Location.X, c.Y - lblSelect.Location.Y, 0)))
            cmsItems.Tag = null;
    }

根据您设计表单的方式,您可能必须调整 MouseEventArgs-Coordinates,才能成功确定 Dropdownhits。我刚刚将 CMenu 控件添加到一个空窗体。

对于解决方法,不要忘记将处理程序添加到 CMenu.cs 的构造函数中:

cmsItems.Closed += cmsItems_Closed;

关于c# - 如何使用按钮单击来切换 ContextMenuStrip 显示,同时允许它正常关闭(菜单项单击、失去焦点等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17326200/

相关文章:

c# - 我可以用字符串变量(或其他东西)命名数组的每个元素吗?

c# - WebBrowser.url 在 winform 中有时不工作

c# - 关于在 C# 中运行时关闭窗体

wpf - 从用户控件外部绑定(bind)到命令

wpf - 设置绑定(bind)到 WPF 用户控件内的自定义 DependencyProperty

XAML,将 Width 和 Height 属性绑定(bind)到其他控件的相同属性

c# - 具有持久性的网页导航 - ASP.NET C#

c# - Xamarin 从位图中获取错误的颜色

c# - 是C#编译器还是CLR禁止多重继承

c# - 如何将自定义对象列表绑定(bind)到 ComboBox?