Hello. I have a combobox in my project. When I write like this:
var combobox = new ComboBox();
var list= new List<ListBoxKeyValue>();
var row= new ListBoxKeyValue() { Key = “dropDownRow”, Value = “dropDownRow” };
list.Add(row);
combobox.DataSource = list;
list appears in the dropdown list, however the first element of my list is always selected by default. I want my text remains to remain empty and selectedItem to remain null. Is there a way to fix it?
Hi Dmitry,
This is the same behavior as in WinForms. I would recommend setting the SelectedIndex to -1 or the SelectedItem to null after setting your DataSource and handling the case specifically in your SelectedIndexChanged event handler. You can also do ResetText() to clear the text.
Hope that helps, let me know if you have any other questions!
Best,
Levie
public class ListBoxKeyValue
{
public string Key;
public string Value;
public bool IsDateTime;
public override string ToString()
{
if (string.IsNullOrEmpty(Value))
return Key;
else
return Value;
}
}
Please login first to submit.