Listbox.SelectedItem = value does not work for me. Actually I want to change selection programmatically. I do not have the index of the Listbox to be selected but the Text only. Actually the displaymember is provided by combo box. when the selection changes in the combo box the resulting changes is to be reflected on listbox as well.
void PopulateList()
{
this.list.DataSource = (IList)ClassDsnManager.GetDataSourceNames();
this.list.DisplayMember = "Name";
this.list.ValueMember = "Driver";
}
void ComboSelectedIndexChanged(object sender, EventArgs e)
{
if (Combo.SelectedIndex != -1)
{
ClassDatabase selecteditem = (ClassDatabase)Combo.SelectedItem;
source.Text = selecteditem.source;
string destination= selecteditem.SqlConn;
if (!string.IsNullOrEmpty(destination))
{
string[] connectionValue = connection.Split(Convert.ToChar(";"));
string dsnName = connectionValue[0].Substring(4, connectionValue[0].Length - 4);
// this is the list box whose value i want to set
lbDSN.SelectedItem = dsnName;
}
}
}
Solution1: In the
SelectedIndexChangedofComboboxsimply writeSolution2: Change the
DropDownStyleofComboboxtoSimple. It will show theComboboxlike aListbox.