2012-08-09 79 views
0

我有2個數據成員的類供應商SupID和供應商名稱和1構造函數然後我將此對象添加到Form1加載()時供應商列表。如何從綁定源中刪除對象不影響列表中的對象

Dim lst As New List(Of Supplier) 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     AddList() 
    End Sub 

    Public Sub AddList() 
     lst.Add(New Supplier("Sup1", "Supplier1")) 
     lst.Add(New Supplier("Sup2", "Supplier2")) 
     lst.Add(New Supplier("Sup3", "Supplier3")) 
     lst.Add(New Supplier("Sup4", "Supplier4")) 
     lst.Add(New Supplier("Sup5", "Supplier5")) 
    End Sub 

然後我想通過它的新的即時構造函數來發送LST到窗體2,當我點擊發送按鈕:

Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click 
     Dim frm As New Form2(lst) 
     frm.Show(Me) 
    End Sub 

接着在窗口2,這樣的代碼:

Dim lst As New List(Of Supplier) 
    Dim bs As BindingSource 

    Public Sub New(ByVal lst As New List(Of Supplier) 
     Me.InitializeComponent() 
     Me.lst = lst 
     bs = new BindingSource(lst,nothing) 
    End Sub 

而且那麼我想從bs中刪除物體:

Public Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click 
     bs.RemoveCurrent() 
    End Sub 

問題是,當我從Form2中的bs中刪除對象時,Form1中的lst中的對象也受到影響。那麼,如果我不希望Form1中的列表產生影響,該怎麼辦?

預先感謝您....

回答

1

嘗試在Form2更改代碼這樣:

Dim lst As List(Of Supplier) 
Dim bs As BindingSource 

Public Sub New(ByVal lst As List(Of Supplier)) 
    Me.InitializeComponent() 
    Me.lst = New List(Of Supplier)(lst) 
    bs = new BindingSource(Me.lst, Nothing) 
End Sub 
+0

是的,它的工作原理。但你能解釋一下爲什麼你這樣寫:Me.list =新列表(供應商)(1st) – Ericton 2012-08-09 05:19:27

+0

這是寫入'lst'(參數)列表副本到'lst'(模塊級別)變量,而不僅僅是直接賦值。 – Enigmativity 2012-08-09 05:43:57