I understand that it is very late ... however, having just implemented it, I will leave it here, hoping that it will help someone.
Add a KeyChar event handler (in my case, the list is named lbxFieldNames):
private void lbxFieldNames_KeyPress(object sender, KeyPressEventArgs e)
{
IncrementalSearch(e.KeyChar);
e.Handled = true;
}
(: e.Handled = true;, listbox " , char" , , , .)
IncrementalSearch:
private void IncrementalSearch(char ch)
{
if (DateTime.Now - lastKeyPressTime > new TimeSpan(0, 0, 1))
searchString = ch.ToString();
else
searchString += ch;
lastKeyPressTime = DateTime.Now;
var item = lbxFieldNames
.Items
.Cast<string>()
.Where(it => it.StartsWith(searchString, true, CultureInfo.InvariantCulture))
.FirstOrDefault();
if (item == null)
return;
var index = lbxFieldNames.Items.IndexOf(item);
if (index < 0)
return;
lbxFieldNames.SelectedIndex = index;
}
, , , , TimeSpan if.
,
private string searchString;
private DateTime lastKeyPressTime;
, .