将键击发送到其他控件

时间:2009-08-12 04:34:08

标签: c# forwarding keystroke

对你们来说很容易。

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

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

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

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

5 个答案:

答案 0 :(得分:13)

SendKeys.Send()方法。

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

以下是您可以选择列表项的代码。

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;
        }

答案 1 :(得分:1)

您可以使用数据绑定

            listBox1.DataBindings.Add("DataSource", textBox1, "Text", true, DataSourceUpdateMode.OnPropertyChanged).
            Format += (sender, e) =>
            {
                e.Value = _strings.FindAll(s => s.StartsWith((string) e.Value));
            };

答案 2 :(得分:1)

    private void textBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
        {
            // Setting e.IsInputKey to true will allow the KeyDown event to trigger.
            // See "Remarks" at https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx
            e.IsInputKey = true;
        }
    }

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        string send = "";
        if (e.KeyCode == Keys.PageUp)
        {
            send = "PGUP";
        }
        else if (e.KeyCode == Keys.PageDown)
        {
            send = "PGDN";
        }
        else if (e.KeyCode == Keys.Up)
        {
            send = "UP";
        }
        else if (e.KeyCode == Keys.Down)
        {
            send = "DOWN";
        }
        if (send != "")
        {
            // We must focus the control we want to send keys to and use braces for special keys.
            // For a list of all special keys, see https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx.
            listBox.Focus();
            SendKeys.SendWait("{" + send + "}");
            textBox.Focus();
            // We must mark the key down event as being handled if we don't want the sent navigation keys to apply to this control also.
            e.Handled = true;
        }
    }

答案 3 :(得分:0)

在我们的wpf应用程序中,我们有一个过滤列表框的文本框,我们使用previewkeyup事件。在代码中,我们可以检查按下了什么键(没有我的代码在我面前,它类似于e.Key == Key.UpArrow,无论哪种方式都有C#中的内置类)。如果它是热键之一,我们会相应地操纵用户控件。

对于列表框,我们将它扔进用户控件并实现了一个接口,称为NavigateableListbox或类似的东西,强制它实现MoveUp(),MoveDown(),PageUp(),PageDown()等所以文本框事件只是说e.Key = Key.UpArrow {mylistbox.MoveUp()}

答案 4 :(得分:0)

SendKeys.Send()方法并非在所有情况下都有效,因为Send()方法可以寻址到另一个控件!

在2020年,要确保100%的最佳解决方案是使用SendMessage()函数。

在VB.Net中,我使用以下代码

Public Declare Auto Function SendMessage Lib "user32.dll"
    ( ByVal hWnd As IntPtr
    , ByVal wMsg As Int32
    , ByVal wParam As Int32
    , ByVal s As Int32
    ) As Int32

Private Const WM_CHAR As Int32 = &H102

SendMessage(txtInput.Handle, WM_CHAR, AscW(sValue.Chars(0)), 0)

txtInput是示例Textbox控件。

在我的情况下,我单击了<div>控件中定义的html WebView元素,该元素在单击或鼠标移过时会改变颜色。当我单击此<div>时,将调用ScriptNotify()VB.Net方法,以使用Textbox函数在SendMessage()中输入一些字符。

在旧的解决方案中,我认为有时WebView在调用SendKeys.Send()方法之前会获得焦点,因此传播的密钥不会发送到txtInput文本框。

您可以使用旧的解决方案,但是请确保有一天,SendKeys.Send()函数会将您想要的内容发送到另一个句柄。