I'm trying to make it so when the user types in the combobox, the combobox will try to find the first item that matches the search value completely. If thats not possible it will try to find the first one that contains the search value. If its neither of the before mentioned it will turn red. Now I have that part figured out and working, but the issue I have is that when the user would try to backspace the search will trigger again and thus it selects a row again most of the times. How can I make it that it won't search after a backspace, or prevent it from selecting the index if the user is trying to backspace. This is the code im using:
private void BestelIndexSearch(object sender, EventArgs e)
{
ComboBox Cmbbx = sender as ComboBox;
int index = -1;
string searchvalue = Cmbbx.Text;
if (Cmbbx.Text != "")
{
for (int i = 0; i < Cmbbx.Items.Count; i++)//foreach replacement (not possible with combobox)
{
//search for identical art
if (Cmbbx.Items[i].ToString().Equals(searchvalue))
{
index = Cmbbx.Items.IndexOf(searchvalue);
break;//stop searching if it's found
}
//search for first art that contains search value
else if (Cmbbx.Items[i].ToString().Contains(searchvalue) && index == -1)
{
index = Cmbbx.FindString(searchvalue);
break;//stop searching if it's found
}
}
}
//if nothing found set color red
if (index == -1)
{
Cmbbx.BackColor = Color.Red;
}
//if found set color white, select the item
else
{
Cmbbx.BackColor = Color.White;
Cmbbx.SelectedIndex = index;
}
//select text behind cursor
Cmbbx.SelectionStart = searchvalue.Length;
Cmbbx.SelectionLength = Cmbbx.Text.Length - searchvalue.Length;
}
The code is set to trigger on the TextChanged
event and it is bound to multiple comboboxes. If anyone could help me it would be appriciated.