2016-04-25 67 views
0

這是在列表框中的數據,從數據庫我需要一個組合框的選擇項添加到選定項目結束在列表框中

Johnie Black 
    Sarah Smith 
    Carol Willis 
    Maggie Dubois 

這是在組合中的數據專欄

(M) 
    (F) 

我要選擇列表框中的名稱,然後,當我繼續從下拉框中選擇性別我選擇必須添加到所選

例如名稱末尾的值。

卡羅爾·威利斯(F)

這是我曾嘗試:

private void Form1_Load(object sender, EventArgs e) {                    this.namesTableAdapter.Fill(this.namesDataSet.names); 
    comboBox1.Items.Add("(M)"); 
    comboBox1.Items.Add("(F)"); 
    comboBox1.SelectedIndex = 0; 
    listBox1.SelectedIndex = 0; 
} 
//The code above loads the items into the comboBox 
//For the lisbox I connected to the database using the option "Use Data Bound Items" 

任何形式的幫助將不勝感激

+0

好的,那麼你有什麼嘗試? – BugFinder

+0

我試過comboBox.SelectedValue = listBox1.SelectedValue – Destiny

+0

我也試過使用SelectedIndexChanged事件,但沒有任何工作 – Destiny

回答

0

這應該指向你到正確的方向:

public ListBox lbNames = new ListBox(); 
public ComboBox cbxGender = new ComboBox(); 

// combobox selected index changed event 
private void cbxGender_SelectedIndex_Changed(object sender, EventArgs e) 
{ 
    // check if there are selected items 
    if(lbNames.SelectedItems.Count == 1 && cbxGender.SelectedItem != null) 
    { 
     // replace previous added gender 
     Regex.Replace(lbNames.SelectedItem.ToString(), @".+(\([MF]\))", ""); 
     // append new gender 
     lbNames.Items[lbNames.SelectedIndex] = lbNames.SelectedItem.ToString() + cbxGender.SelectedItem.ToString(); 
    } 
} 

沒有測試過,只是一個提示。

+0

謝謝你,會嘗試一下 – Destiny

0
listBox1.Items[listBox1.SelectedIndex] = listBox1.Items[listBox1.SelectedIndex] + comboBox1.SelectedItem.ToString(); 
+0

謝謝你,會嘗試一下 – Destiny

+0

如果你點擊多次,這個會一遍又一遍地追加性別。就像'卡羅爾威利斯(F)(F)(F)(F)'。 – C4u

0

像這樣的事情可以做的伎倆:

private void comboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     foreach (ListViewItem item in listView.SelectedItems) 
     { 
      if (comboBox.SelectedItem != null) 
       item.Text += " " + comboBox.SelectedItem.ToString(); 
     } 
    } 

不要忘記雙擊/在表格組合框的屬性添加「的SelectedIndexChanged」事件。

+0

謝謝你會試試 – Destiny

+0

如果你點擊多次,這個會一遍又一遍地追加性別。就像'卡羅爾威利斯(F)(F)(F)(F)'。 – C4u

相關問題