2013-03-06 97 views
-1

我想在以下2個集合之間移動Item對象。VB .Net列表框和對象集合

Private ItemsInRoom As New List(Of CItem) 

Private Inv As New List(Of CItem) 

我希望通過2個ListBoxes來完成。 1是庫存,另一個是物品清單。我怎樣才能做到這一點。

CItem類有幾個成員,只有該項目的名稱需要顯示在ListBox。我已經呆了好幾個小時了,但我什麼都不能工作。這個解釋對我所要做的事情是否有意義?如果沒有,我還能解釋什麼,以便有人可以幫助我?

+1

你能告訴你已經嘗試了什麼。 – 2013-03-06 22:27:01

+0

問題是顯示問題,還是在編譯將項目從一個列表移動到另一個列表的管道時遇到問題? – ja72 2013-03-07 04:18:06

回答

0

在您的CItem類中,您需要覆蓋ToString()函數。這將得到名稱顯示在listbox

Public Class CItem  
    Public Overrides Function ToString() As String 
     Return Me.Name 
    End Function 
    'etc... 
End Class 
0

我想你想要的是這樣的:

Form

這是用下面的代碼來實現:

Option Explicit On 

Public Class Form1 

    Private ItemsInRoom As New List(Of CItem) 
    Private ItemsInInv As New List(Of CItem) 

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) 
     MyBase.OnLoad(e) 

     ItemsInInv.Add(New CItem(1001, "Egret")) 
     ItemsInInv.Add(New CItem(1002, "Dove")) 
     ItemsInInv.Add(New CItem(1003, "Hawk")) 

     UpdateBindings() 
    End Sub 

    Public Function CheckOut(ByVal item As CItem) As Boolean 
     If item IsNot Nothing Then 
      ItemsInInv.Remove(item) 
      ItemsInRoom.Add(item) 
      Return True 
     End If 
     Return False 
    End Function 

    Public Function CheckIn(ByVal item As CItem) As Boolean 
     If item IsNot Nothing Then 
      ItemsInRoom.Remove(item) 
      ItemsInInv.Add(item) 
      Return True 
     End If 
     Return False 
    End Function 

    Public Sub UpdateBindings() 
     itemsInInvListBox.BeginUpdate() 
     itemsInInvListBox.DataSource = Nothing 
     itemsInInvListBox.DataSource = ItemsInInv 
     itemsInInvListBox.DisplayMember = "Name" 
     itemsInInvListBox.EndUpdate() 
     itemsInInvListBox.Refresh() 

     itemsInRoomListBox.BeginUpdate() 
     itemsInRoomListBox.DataSource = Nothing 
     itemsInRoomListBox.DataSource = ItemsInRoom 
     itemsInRoomListBox.DisplayMember = "Name" 
     itemsInRoomListBox.EndUpdate() 
     itemsInRoomListBox.Refresh() 
    End Sub 

    Private Sub itemsInInvListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsInInvListBox.SelectedIndexChanged 
     checkOutButton.Enabled = itemsInInvListBox.SelectedIndex <> -1 
    End Sub 

    Private Sub itemsInRoomListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles itemsInRoomListBox.SelectedIndexChanged 
     checkInButton.Enabled = itemsInRoomListBox.SelectedIndex <> -1 
    End Sub 

    Private Sub checkOutButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkOutButton.Click 
     Dim item As CItem = CType(itemsInInvListBox.SelectedItem, CItem) 
     If CheckOut(item) Then 
      UpdateBindings() 
     End If 
    End Sub 

    Private Sub checkInButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkInButton.Click 
     Dim item As CItem = CType(itemsInRoomListBox.SelectedItem, CItem) 
     If CheckIn(item) Then 
      UpdateBindings() 
     End If 
    End Sub 
End Class 


Public Class CItem 
    Public Sub New(ByVal item_id As UInteger, ByVal item_name As String) 
     Me.m_id = item_id 
     Me.m_name = item_name 
    End Sub 
    Private m_name As String 
    Public Property Name() As String 
     Get 
      Return m_name 
     End Get 
     Set(ByVal value As String) 
      m_name = value 
     End Set 
    End Property 

    Private ReadOnly m_id As UInteger 
    Public ReadOnly Property ID() As UInteger 
     Get 
      Return m_id 
     End Get 
    End Property 

End Class