【问题标题】:RemoveAt index c#RemoveAt 索引 c#
【发布时间】:2012-12-04 23:51:31
【问题描述】:

我有一个项目正在努力解决如何从列表中删除。

我要删除的列表项是用户从列表框中选择的索引中的任何项。

该项目有一个名为 Pickcuplist 的列表类和 2 个 GUI,一个用于主窗体,一个用于拾取窗体。拾取表单是删除按钮所在的位置,但带有选定索引的列表框位于主表单上。

我找到了 List.RemoveAt 但是我似乎无法让它工作。

物品被添加到列表中,就像从拾取表单中一样:

txtCustName.Text = thePickup.name;
txtCustAddress.Text = thePickup.address;
txtArrival.Text = thePickup.arrival.ToString();
txtDaddress.Text = thePickup.Daddress;
txtDeliveryName.Text = thePickup.Dname;
LblType.Text = thePickup.type;

这是主表单添加的代码

  /*
            * This method creates a new pickup object, allows the user to
            * enter details and adds it to the List
            *  
            */

        Pickupform.pickup = new Pickups();
        //New Visit- note added to the pickupform object

        Pickupform.ShowDialog();
        //Show the pickupForm. ShowDialog ensures that the form has the exclusive focus until it is closed.

        if (Pickupform.pickup != null)
        //if null then the "cancel" button was pressed
        {
            Pickups newpic = Pickupform.pickup;
            //Get the Pickup object from the form

            thePickup.addPickups(newpic);
            //Add the visit to the list
        }
        updateList();
        //Update the list object to reflect the Pickups in the list

【问题讨论】:

  • 我看不到您要删除的位置...
  • RemoveAt 到底有什么不好的地方?
  • 移除物品的代码在哪里?
  • 如果你想删除一个项目,你需要一个 thePickup.RemoveAt(index)。
  • @TAM 那行代码 (theList.listVisits().RemoveAt(int index);) 不是有效的 C#,它甚至还不够接近有效,无法知道从哪里开始纠正它。在继续之前,您可能需要复习基本的 C# 编程概念和语法。

标签: c# list


【解决方案1】:

看看这个链接 (http://www.dotnetperls.com/removeat)

关键是这一点:

var list = new List<string>();
list.Add("dot");
list.Add("net");
list.Add("perls");

list.RemoveAt(1); <-- See, you pass in an integer

因此,在您的情况下,您将有一个事件或触发删除的东西,此时您需要找到要删除的项目的索引并调用该方法。如果您对此感到困扰,则应该考虑做一些教程,这是非常基础的。

【讨论】:

    【解决方案2】:

    转到代表具有删除按钮的Form 的类。

    查找附加到此按钮的Click 事件的事件处理程序。

    如果您可以从此类访问pickupList,请从那里致电RemoveAt

    如果您无权访问它,请找到将列表传递给其他表单的方法。例如,如果带有“删除”按钮的表单是从另一个表单创建的,那么您可以将列表作为构造函数参数传递,并将其保存在带有“删除”按钮的表单的字段中。

    【讨论】:

      猜你喜欢
      • 2012-10-07
      • 2022-11-14
      • 2019-06-07
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多