This is the data in the list box, from a database
Johnie Black
Sarah Smith
Carol Willis
Maggie Dubois
This is the data in the combo Box
(M)
(F)
I want to select a name in the listbox then when I proceed to select the gender from the comboBox the value I select must be added to the end of the name that is selected
example.
Carol Willis(F)
This is what I have tried:
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"
Any form of help will be appreciated
答案 0 :(得分: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();
}
}
没有经过测试,只是一个提示。
答案 1 :(得分:0)
listBox1.Items[listBox1.SelectedIndex] = listBox1.Items[listBox1.SelectedIndex] + comboBox1.SelectedItem.ToString();
答案 2 :(得分:0)
这样的事情可以解决问题:
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in listView.SelectedItems)
{
if (comboBox.SelectedItem != null)
item.Text += " " + comboBox.SelectedItem.ToString();
}
}
不要忘记在表单上的ComboBox属性中双击/添加“SelectedIndexChanged”事件。