2016-04-25 66 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

相关问题