Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在数十个列表框中检查selectedIndex以进行编辑/删除?_C#_Winforms - Fatal编程技术网

C# 如何在数十个列表框中检查selectedIndex以进行编辑/删除?

C# 如何在数十个列表框中检查selectedIndex以进行编辑/删除?,c#,winforms,C#,Winforms,我的表格上有几十个列表框。我希望能够只使用一个编辑按钮和一个删除按钮编辑/删除这些列表框中的项目。是否应该创建一个循环,这样我就不必为每个列表框编写if语句?也许是定制的方法?这里有点迷路了 谢谢你的意见 这是要编辑的其中一个列表框的代码: private void btnEdit_Click(object sender, EventArgs e) { //Edit an item in the list box //If there are no ap

我的表格上有几十个列表框。我希望能够只使用一个编辑按钮和一个删除按钮编辑/删除这些列表框中的项目。是否应该创建一个循环,这样我就不必为每个列表框编写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();
            }
        }

您可以在foreach循环中使用Form.Controls集合来检查和操作列表框。 您可以做的另一件事是将相同的selectedindexchanged事件分配给所有要操作的列表框。“sender”参数保存引发事件的对象。您可以将其强制转换为列表框并对其进行操作

编辑:您的基本问题是不知道上次选择了哪个列表框。您需要定义一个变量,该变量将保留用户输入的最后一个列表框(使用Enter事件),并使用该变量从列表框中删除该项。 例如:

您需要将要操作的所有列表框的Enter事件设置为aListBoxName\u Enter方法


如果只想从所有列表框中删除相同的索引,只需循环表单的控件集合,并执行
if(control is ListBox)((ListBox)control.RemoveAt(index)

正如ThunderGr建议的那样,您可以将相同的事件处理程序附加到所有列表框,以便应用相同的逻辑。当涉及到迭代时(最初分配事件处理程序,然后作为一个整体处理集合),我会将每个控件的
Tag
属性设置为相关的属性(例如“List”),然后您可以执行以下操作:

foreach(var control in Controls.Where(c => c.Tag == "List"))
{
    // work with control here (casting to ListBox appropriately).
}
您甚至可以将其作为表单的方法,返回
IEnumerable
,如下所示:

public IEnumerable<ListBox> ListBoxes
{
    get
    {
        return Controls.Where(c => c.Tag == "List").Cast<ListBox>();
    }
}
公共IEnumerable列表框
{
得到
{
返回控件。其中(c=>c.Tag==“List”).Cast();
}
}

您正在从
任命列表
列表中删除另一个列表项(
lstDayView

替换此项:

appointmentList.RemoveAt(lstDayView.SelectedIndex);
包括以下内容:

appointmentList.RemoveAt(appointmentList.SelectedIndex); 

请你们用一些例子解释清楚,如果可能的话,分享你们的代码。我想你们需要的是一个控件列表。然后,您可以迭代此列表以查找活动列表,而不是大型的
if(box1)else if(box2)…
type语句。当然,你甚至可以从表单内容自动生成这个列表,但是如果你不想考虑列表框的话,这可能是有风险的。列表框并不像RadioButtons那样互斥。因此,这将是令人困惑的,因为无法确定哪个列表框是“当前”列表框,因为它们可以同时进行选择,而且它们看起来都一样。从技术上讲,您可以跟踪表单级别变量中最后单击/更改的列表框,并在编辑/删除例程中使用它。一些可能性…以某种方式使“当前”列表框显示不同。在一个列表框中进行选择时,使所有其他列表框取消选择当前项目。感谢您的帮助。我忘了那部分。我们会解决这个问题,看看它是否有效。
appointmentList.RemoveAt(appointmentList.SelectedIndex);