一个combobox如果被选中了,按键盘的上下键它就会自动改变下拉框选中的值,怎么让上下键不改变combobox的选中值
------解决方案--------------------------------------------------------
重写KeyPress与ProcessCmdKey,
- C# code
private void comboBox2_KeyPress(object sender, KeyPressEventArgs e) { if ((int)e.KeyChar < 32) // 控制键 { return; } e.Handled = true; return; }
------解决方案--------------------------------------------------------
在KeyDown事件中写e->Handled = true;
------解决方案--------------------------------------------------------
在你的窗体中增加
BOOL PreTranslateMessage(MSG* pMsg);
实现如下:
BOOL CPreTranslateMessageDlg::PreTranslateMessage(MSG* pMsg)
{
HWND comboboxHwnd = ::GetDlgItem(GetSafeHwnd(),IDC_COMBO1);
HWND edithwnd = ::GetDlgItem(GetSafeHwnd(),IDC_EDIT1);
if (pMsg->message == WM_KEYDOWN)
{
HWND temphwnd = GetFocus()->m_hWnd;
HWND editHwnd = ::GetWindow(comboboxHwnd,GW_CHILD);
if (pMsg->hwnd == editHwnd && (pMsg->wParam == VK_UP || pMsg->wParam == VK_DOWN))
{
return TRUE;
}
}
return CDialog::PreTranslateInput(pMsg);
}
这样按方向键就没反应捏。原理也说一下
combobox是个组合控件,响应KEYDOW KEYUP 的是哪个EDIT控件
只要截获哪个EDIT的键盘消极,不让处理就行。
------解决方案--------------------------------------------------------
响应KeyDown事件:
- C# code
private void comboBox1_KeyDown(object sender, KeyEventArgs e){ if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) e.Handled = true;}
------解决方案--------------------------------------------------------
KeyPress事件不能获取或设置以下键.
Tab 键、Insert、Delete、Home、End、Page Up 、 Page Down、F1-F2、Alt、箭头键。
KeyDown事件能获取、设置这些键值。
- C# code
private void comboBox1_KeyDown(object sender, KeyEventArgs e){ if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) e.Handled = true;}