Send keystroke to other control

此生再无相见时 提交于 2019-11-29 09:42:38

SendKeys.Send() Method.

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

Here is code through which you can select a list item.

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

You can use Data Binding

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

In our wpf app we have a textbox that filters a listbox, we use the previewkeyup event. Inside the code, we can check what key was pressed (don't have my code in front of me, it's something like e.Key == Key.UpArrow, either way there's a built in class in C# for this). If it's one of the hot keys, we manipulate the user control accordingly.

For the listbox we tossed it into a user control and implemented an interface, called it NavigateableListbox or something like that, forced it to implement MoveUp(), MoveDown(), PageUp(), PageDown() etc so the textbox event just says if e.Key = Key.UpArrow { mylistbox.MoveUp() }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!