Send keystroke to other control
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;
}
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;
}
}
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));
};