我的表单上有几十个列表框。我希望能够仅使用一个编辑按钮和一个删除按钮从这些列表框中编辑/删除项目。是否应该创建一个循环,这样我就不必为每个列表框编写一个 if 语句?也许是自定义方法?有点迷失在这里。
感谢您的输入。
这是用于编辑的列表框之一的代码:
private void btnEdit_Click(object sender, EventArgs e)
{
//Edit an item in the list box
//If there are no appointments OR no appointment is selected, inform the user and cancel the operation
if ((!(appointmentList.Count > 0)) || lstDayView.SelectedIndex == -1)
{
MessageBox.Show("Error! You need to select an appointment!");
return;
}
else
{
int index = lstDayView.SelectedIndex;
var myForm = new Form2(appointmentList[index] as Appointment);
if (myForm.ShowDialog() == DialogResult.OK && myForm.Tag is Appointment)
{
Appointment appoint = myForm.Tag as Appointment;
//lstDayView.Items.RemoveAt(index);
appointmentList.RemoveAt(index);
appointmentList.Insert(index, appoint);
//appoint.toListBox(lstDayView, index);
this.setCal();
}
}
这是用于删除:
private void btnDeleteApp_Click_1(object sender, EventArgs e)
{
if ((!(appointmentList.Count > 0)) || lstDayView.SelectedIndex == -1)
{
MessageBox.Show("Error! You need to make and/or select an appointment!");
return;
}
else
{
if (MessageBox.Show("Are you sure you wish to delete?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
appointmentList.RemoveAt(lstDayView.SelectedIndex); //Issue this is removed the index number from the list not the appointmentList
this.setCal();
}
}