关闭时如何从键箭头 Up\Down 禁用组合框的滚动?
How to disable scrolling of comboBox from keys arrow Up\Down when it closed?
箭头键应该只滚动图片框(放置在面板中)。它工作正常。
但它也会滚动浏览 comboBox 项目,尽管它是关闭的 (droppedUp)。
如何禁用它?
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.DroppedDown = false;
comboBox1.Items.Add("111");
comboBox1.Items.Add("222");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down)
{
Point current = panel1.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 50);
panel1.AutoScrollPosition = scrolled;
}
return base.ProcessCmdKey(ref msg, keyData);
}
我读过 Control.PreviewKeyDown 事件:
preview key event
还找到了另一个例子:
preview key event
但我无法理解它在我的案例中是如何使用的。
正如 Hans Passant 评论的那样,您必须 return 正确。此外,添加一些其他键可能会更改组合框中的选定项目:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down ||
keyData == Keys.Up ||
keyData == Keys.PageDown ||
keyData == Keys.PageUp)
{
Point current = panel1.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 50);
panel1.AutoScrollPosition = scrolled;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
箭头键应该只滚动图片框(放置在面板中)。它工作正常。 但它也会滚动浏览 comboBox 项目,尽管它是关闭的 (droppedUp)。 如何禁用它?
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.DroppedDown = false;
comboBox1.Items.Add("111");
comboBox1.Items.Add("222");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down)
{
Point current = panel1.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 50);
panel1.AutoScrollPosition = scrolled;
}
return base.ProcessCmdKey(ref msg, keyData);
}
我读过 Control.PreviewKeyDown 事件: preview key event 还找到了另一个例子: preview key event 但我无法理解它在我的案例中是如何使用的。
正如 Hans Passant 评论的那样,您必须 return 正确。此外,添加一些其他键可能会更改组合框中的选定项目:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Down ||
keyData == Keys.Up ||
keyData == Keys.PageDown ||
keyData == Keys.PageUp)
{
Point current = panel1.AutoScrollPosition;
Point scrolled = new Point(current.X, -current.Y + 50);
panel1.AutoScrollPosition = scrolled;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}