2013-03-06 59 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