rev*_*ice 3 .net c# combobox winforms drop-down-menu
如何在ComboBox中隐藏DropDownList?
我想只使用ComboBox来显示文本.
这个控件看起来不错,对我来说比TextBox加Button更好.
因此必须启用控制,但不包含任何项目.
当用户单击箭头(或alt +向下键)时,DropDownList应该不显示,因为从自定义DataGridView中选择错误以便在ComboBox中填充文本.
编辑.替代解决方案是将DropDownHeight设置为1,在单击控件后仅显示1个像素行.
编辑.真正的解决方 答案如下
您可以在子类中拦截导致框下拉的消息.以下代码段定义了一个控件NoDropDownBox,它忽略了导致组合框下拉的鼠标单击:
public class NoDropDownBox : ComboBox
{
public override bool PreProcessMessage(ref Message msg)
{
int WM_SYSKEYDOWN = 0x104;
bool handled = false;
if (msg.Msg == WM_SYSKEYDOWN)
{
Keys keyCode = (Keys)msg.WParam & Keys.KeyCode;
switch (keyCode)
{
case Keys.Down:
handled = true;
break;
}
}
if(false==handled)
handled = base.PreProcessMessage(ref msg);
return handled;
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case 0x201:
case 0x203:
break;
default:
base.WndProc(ref m);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)