C# 在文本框中写入时,如何将箭头键集中在ListView上?
我有一个简单的Windows窗体,其中包含一个文本框和一个列表视图 我希望允许用户在关注ListView时将文本写入文本框(在文本框中键入文本,同时使用键盘箭头键控制ListView)C# 在文本框中写入时,如何将箭头键集中在ListView上?,c#,winforms,focus,C#,Winforms,Focus,我有一个简单的Windows窗体,其中包含一个文本框和一个列表视图 我希望允许用户在关注ListView时将文本写入文本框(在文本框中键入文本,同时使用键盘箭头键控制ListView) 如何执行此操作?为表单实施IMessageFilter,并在适当时更改预期收件人: public partial class form1 : Form, IMessageFilter { private NativeWindow nw = null; public form1() {
如何执行此操作?为表单实施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;
}
}
你看过文本框上的键盘事件了吗?我用这个方法处理由PPT VSTO加载项按钮触发的表单。它只有在PPT启动并点击按钮时才第一次起作用。但之后就不起作用了。对如何解决这个问题有何评论?谢谢。你想设什么圈套?按钮是否单击弹出窗体?