2012-12-04 18 views
-4

我有一個項目,我正在努力如何從列表中刪除。RemoveAt index c#

我想要刪除的列表項目是用戶從列表框中選擇的索引處的任何項目。

該項目有一個名爲Pickcuplist的列表和一個用於主窗體的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 
+7

我看不到你要刪除的位置... – Chris

+1

究竟是不是有關'RemoveAt'的工作? –

+2

去除物品的代碼在哪裏? – ChrisF

回答

0

看看這個鏈接(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 

因此,在您的情況下,您將遇到事件或觸發刪除的事情,此時您需要找到要刪除的項目的索引並調用該方法。如果你爲此苦苦掙扎,你應該考慮做一些教程,這是非常基本的。

0

轉到代表具有刪除按鈕的Form的類。

查找附加到此按鈕的Click事件的事件處理程序。

如果您有權訪問此類中的pickup列表,請從那裏調用RemoveAt

如果您無法訪問它,請找到將列表傳遞給其他表單的方法。例如,如果帶有Delete按鈕的表單是從其他表單創建的,那麼您可以將該列表作爲構造函數參數傳遞,並使用Delete按鈕將其保存在表單中的字段中。