0
votes

EDIT This has nothing to do with SelectedIndex property... If the default combobox text is "ABCD" and there's an item that starts with "ABCD", the combobox will find that item and select it. How do I prevent that from happening?

Original

When I click ComboBox1 (Windows Forms - Visual Studio), the system will search the database for information to insert into a ComboBox (string[,] Intel is where this information is stored). Before the Click event is called, the ComboBox is with SelectedIndex = -1. This is how it adds Intel into this specific combobox:

for (int Dim1 = 0; Dim1 < Intel.GetLength(0); Dim1++)
{
    ComboBox1.Items.Add(Intel[Dim1, 1]);
}

My problem is: Once I add the first item, ComboBox1.SelectedIndex is set to 0. Even if I use ComboBox1.SelectedIndex = -1;, it still assumes it as 0. Same with ComboBox1.SelectedValue = null; or ComboBox1.Text = null;. By TextBox1.Text = ComboBox1.SelectedIndex;, TextBox1.Text will display -1.

I've done this with another ComboBox, and it works... It does set the selectedindex at -1, and the user will see the default text I want, which is not included in the items - and it can't. So, I'm thinking this must be some property I've set wrong.

1
are you setting the DataSource property of the combobox? that could cause this issue. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item. - msdn.microsoft.com/en-us/library/…ps2goat
Change of plans. Check question again... Such a simple thing.... How do I prevent the combobox from selecting an item that relates to the Text value?C. Carrasquinho
you should show your combobox configuration i.e. "default text" and also anywhere where you might be preselecting an optionDaniDev

1 Answers

0
votes

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.selectedindex?redirectedfrom=MSDN&view=netcore-3.1#remarks

To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.

See this image: