【发布时间】:2015-08-27 02:03:15
【问题描述】:
假设我有以下代码:
private List<Employee> displayEmp;
public Form1()
{
InitializeComponent();
displayEmp = new List<Employee>();
}
在我的添加按钮处理程序中:
private void button1_Click(object sender, EventArgs e)
{
string[] selection = comboEmail.GetItemText(comboEmail.SelectedItem).Split(',');
Employee add = new Employee(Convert.ToInt32(selection[0]), selection[1], selection[2], selection[3]);
if (!(comboEmail.SelectedIndex == 0))
{
if(!(listEmail.Items.Contains(add))){
displayEmp.Add(add);;
listEmail.DataSource = null;
listEmail.DataSource = displayEmp;
}
else
{
MessageBox.Show(add.ToString() + " Already Added.");
}
}
}
我的删除按钮处理程序:
private void button2_Click(object sender, EventArgs e)
{
int indexRemoval = listEmail.SelectedIndex;
if (indexRemoval != -1)
{
displayEmp.RemoveAt(indexRemoval);
listEmail.DataSource = null;
listEmail.DataSource = displayEmp;
}
}
I have a list of employees in a ComboBox that when selected, I add to a listbox.在我的添加/删除按钮处理程序中,我做对了吗?当您将集合绑定到控件并且想要添加/删除项目时,正确的做法是什么?
【问题讨论】: