-1

I would like to move Item objects between the 2 following collections.

Private ItemsInRoom As New List(Of CItem)

Private Inv As New List(Of CItem)

I would like this to be done through 2 ListBoxes. 1 is the Inventory and the other is the Item list. How can I do this.

The CItem class has several members, only the Name of the item needs to be shown in the ListBox. I have been at this for hours, but I can't get anything to work. Does this explanation make sense in what I'm trying to do? If not, what else can I explain so someone might help me?

4

2 回答 2

0

在您的CItem课程中,您需要覆盖该ToString()功能。这将使名称显示在listbox.

Public Class CItem    
    Public Overrides Function ToString() As String
      Return Me.Name
    End Function
    'etc...
End Class
于 2013-03-07T02:22:43.703 回答
0

我想你想要的是这样的:

形式

这是通过以下代码完成的:

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
于 2013-03-07T05:03:58.740 回答