AutoCompleteSource does not work on multiline TextBox controls.
Wich means you need to make it from scratch:
I would make a ListBox to display the content of your autocomplete:
var listBox = new ListBox();
Controls.Add(listBox);
You need eventhandling on your textbox however this is a bit crude, so i would rewrite it to stop the keyupevent at some point:
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
var x = textBox.Left;
var y = textBox.Top + textBox.Height;
var width = textBox.Width + 20;
const int height = 40;
listBox.SetBounds(x, y, width, height );
listBox.KeyDown += listBox_SelectedIndexChanged;
List<string> localList = list.Where(z => z.StartsWith(textBox.Text)).ToList();
if(localList.Any() && !string.IsNullOrEmpty(textBox.Text))
{
listBox.DataSource = localList;
listBox.Show();
listBox.Focus();
}
}
Now all you need is a handler to set the text in your textBox:
void listBox_SelectedIndexChanged(object sender, KeyEventArgs e)
{
if(e.KeyValue == (decimal) Keys.Enter)
{
textBox2.Text = ((ListBox)sender).SelectedItem.ToString();
listBox.Hide();
}
}
Put in null checks where appropriate
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…