2012-03-13 81 views
0

Listbox.SelectedItem = value對我不起作用。其實我想以編程方式改變選擇。我沒有要選擇的列表框的索引,但只有文本。實際上,顯示器部件由組合框提供。當組合框中的選擇更改時,生成的更改也將反映在列表框中。listbox selected item winform

 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; 

      } 

     } 

    } 
+1

它會幫助,如果你顯示你用來填充列表框的代碼 – 2012-03-13 06:24:46

+0

@JohnArlen你能否看到它我已經粘貼了代碼 – arjun 2012-03-13 06:55:35

回答

2

解決方法1:ComboboxSelectedIndexChanged簡單的寫

listBox1.SelectedItem = comboBox1.SelectedItem.ToString(); 

溶液2:變化ComboboxDropDownStyleSimple。它將顯示Combobox,如Listbox