我有一个包含TextBox和ListView的简单Windows窗体。
我希望允许用户在关注ListView时将文本写入TextBox(在使用键盘箭头键控制ListView的同时在TextBox中键入文本)。
我该怎么做?
答案 0 :(得分:1)
为您的表单实施IMessageFilter并在适当时更改目标收件人:
public partial class form1 : Form, IMessageFilter
{
private NativeWindow nw = null;
public form1()
{
InitializeComponent();
nw = new NativeWindow();
nw.AssignHandle(this.listView1.Handle);
Application.AddMessageFilter(this);
// so you can see the selection moving when the arrow keys are pressed in the TextBox
this.listView1.HideSelection = false;
this.listView1.Items[0].Selected = true;
}
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
public bool PreFilterMessage(ref Message m)
{
if (m.HWnd.Equals(this.textBox1.Handle))
{
switch (m.Msg)
{
case WM_KEYDOWN:
case WM_KEYUP:
switch ((Keys)m.WParam)
{
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Left:
m.HWnd = this.listView1.Handle; // change the handle to the ListView
nw.DefWndProc(ref m); // send the message to the ListView
return true; // suppress handling by the TextBox
break;
}
break;
}
}
return false;
}
}