将键击发送到其他控件

vIc*_*erg 10 c# forwarding keystroke

对你们来说很简单.

我在列表框的顶部有一个文本框.

文本框用于过滤列表框中的数据.

所以...当用户输入文本框时,我想"陷阱"向下/向上/向下/向下翻页键击并将它们发送到列表框.

我知道我可以使用Win32 API并发送WM_KeyDown消息.但必须有一些.NET方法来做到这一点.

ada*_*ost 14

SendKeys.Send()方法.

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            listBox1.Focus();
            SendKeys.Send(e.KeyChar.ToString());
        }
Run Code Online (Sandbox Code Playgroud)

这是您可以通过其选择列表项的代码.

private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox1.AutoCompleteSource=AutoCompleteSource.CustomSource;
            string[] ar = (string[])(listBox1.Items.Cast<string>()).ToArray<string>();
            textBox1.AutoCompleteCustomSource.AddRange(ar);
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            listBox1.Text  = textBox1.Text;
        }
Run Code Online (Sandbox Code Playgroud)

  • 如何告诉SendKeys.Send将击键发送到列表框控件? (3认同)
  • 如果我希望焦点保留在文本框中,我会立即执行 textbox.focus() 吗?奇怪的是 sendkey 没有重载来将控制传递给它...... (2认同)