[英]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.