我在Form_Load里面给ListBox注册了KeyDown事件,代码如下,但是我点击一下界面上的按钮,KeyDown事件就不能触发了,这是为什么?
foreach (System.Windows.Forms.Control control in this.calendarPanel.Controls)
{
if (control is MyListBox)
{
MyListBox listbox = (MyListBox)control;
listbox.KeyDown += new KeyEventHandler(lstBox_KeyDown);
}
}
foreach (System.Windows.Forms.Control control in this.calendarPanel.Controls)
{
if (control is MyListBox)
{
MyListBox listbox = (MyListBox)control;
listbox.KeyDown += new KeyEventHandler(lstBox_KeyDown);
}
}
你可以考虑给FORM加个KeyDown事件..使用 窗体的KeyPreview = true;尝试过KeyPreview = true;了 还是不行啊
单设置为true,估计还要注册一个事件吧
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication46
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); Button B = new Button();
B.Parent = this;
B.Click += new EventHandler(B_Click); ListBox LB = new ListBox();
LB.Parent = this;
LB.Location = new Point(0, 20);
LB.KeyDown += new KeyEventHandler(LB_KeyDown);
} void LB_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("ListBox");
} void B_Click(object sender, EventArgs e)
{
MessageBox.Show("Button");
}
}
}