简体   繁体   English

如何强制DropDownList样式ComboBox仅在用户单击下拉按钮时打开?

[英]How can I force a DropDownList style ComboBox to only open when the user clicks the drop-down button?

In C# .NET 2.0, I have a WinForms ComboBox with ComboBoxStyle DropDownList. 在C#.NET 2.0中,我有一个带有ComboBoxStyle DropDownList的WinForms ComboBox。 However, the drop down appears whenever the user clicks anywhere on the combo box. 但是,只要用户单击组合框中的任何位置,就会显示下拉列表。 Instead, I'd like to have it only open when the user explicitly clicks on the drop down button. 相反,我想只在用户明确点击下拉按钮时才打开它。 When the user clicks on the rest of the combo box, I'd like to just assign it the keyboard focus so he or she can use some keyboard commands on the selected item. 当用户点击组合框的其余部分时,我只想为其分配键盘焦点,以便他或她可以在所选项目上使用某些键盘命令。 What's the best way to do this? 最好的方法是什么?

After some help from the other answers, I arrived at this quick solution: 在得到其他答案的一些帮助之后,我得出了这个快速的解决方案:

public class MyComboBox : ComboBox
{
    public MyComboBox()
    {
        FlatStyle = FlatStyle.Popup;
        DropDownStyle = ComboBoxStyle.DropDownList;
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0201 /* WM_LBUTTONDOWN */ || m.Msg == 0x0203 /* WM_LBUTTONDBLCLK */)
        {
            int x = m.LParam.ToInt32() & 0xFFFF;
            if (x >= Width - SystemInformation.VerticalScrollBarWidth)
                base.WndProc(ref m);
            else
            {
                Focus();
                Invalidate();
            }
        }
        else
            base.WndProc(ref m);
    }
}

You have two issues to consider. 您有两个需要考虑的问题。 The first is rather simple: determine whether the dropdown should be opened or closed. 第一个是相当简单的:确定是否应该打开或关闭下拉列表。 This code can do that: 这段代码可以这样做:

    void comboBox1_MouseClick(object sender, MouseEventArgs e)
    {
        ComboBox combo = sender as ComboBox;
        int left = combo.Width - (SystemInformation.HorizontalScrollBarThumbWidth + SystemInformation.HorizontalResizeBorderThickness);
        if (e.X >= left)
        {
            // They did click the button, so let it happen.
        }
        else
        {
            // They didn't click the button, so prevent the dropdown.
        }
    }

The second issue is more significant -- actually preventing the dropdown from appearing. 第二个问题更为重要 - 实际上阻止了下拉的出现。 The simplest approach is: 最简单的方法是:

comboBox1.DropDownStyle = ComboBoxStyle.DropDown;

But, that allows typing into the box, which you may not want. 但是,这允许输入您可能不想要的框。

I spent about 15 minutes looking at options, and it appears that to prevent the dropdown from appearing and simultaneously prevent the user from typing into the dropdown, you would need to subclass the control. 我花了大约15分钟来查看选项,看起来为了防止下拉列表出现并同时阻止用户输入下拉列表,您需要对控件进行子类化。 That way, you can override OnMouseClick(), and only call the base.OnMouseClick() when they did click on button. 这样,你可以覆盖OnMouseClick(),只有当他们点击按钮时才调用base.OnMouseClick()。 It would look something like this (untested): 它看起来像这样(未经测试):

public class CustomComboBox : ComboBox
{
    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);

        int left = this.Width - (SystemInformation.HorizontalScrollBarThumbWidth + SystemInformation.HorizontalResizeBorderThickness);
        if (e.X >= left)
        {
            // They did click the button, so let it happen.
            base.OnMouseClick(e);
        }
        else
        {
            // They didn't click the button, so prevent the dropdown.
            // Just do nothing.
        }
    }
}

您可以获得鼠标单击的X,Y位置,如果不在下拉“图标”(由于缺少更好的单词),您可以从那里强制折叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式删除 ComboBox 下拉按钮? - How to remove the ComboBox Drop-Down button programmatically? 如何强制PropertyGrid始终显示下拉按钮? - How can you force the PropertyGrid to always show the drop-down button? 除了精确匹配之外,如何在下拉列表中阻止ComboBox中的自动选择? - How can I prevent auto-select in ComboBox on drop-down except for exact matches? 确定 ComboBox 下拉按钮的边框大小 - Determine the size of the bordering rectangle of the ComboBox Drop-Down Button 如何在ListBox的下拉区域添加边框? - How can I add a border to the Drop-down area of a ListBox? 当ComboBox位于DataGridComboBoxColumn内部时,如何在单击其ComboBoxItem-s之一时隐藏ComboBox的下拉菜单? - How to hide the ComboBox's drop-down on click on one of its ComboBoxItem-s when the ComboBox is inside a DataGridComboBoxColumn? 当ComboBox的下拉列表已经显示在屏幕上时,该如何动态更改? - How can I change ComboBox's drop down list dynamically, when it's showed on-screen already? 如何更改组合框控件中的下拉按钮? - How to change drop down button in combobox control? 下拉组合框样式时选择文本 - Text is getting selected when combobox style is drop down 如何仅显示(设置)下拉菜单中的特定项目? - How to display(set) only particular items in drop-down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM