-1

次の 2 つのコレクション間で Item オブジェクトを移動したいと考えています。

Private ItemsInRoom As New List(Of CItem)

Private Inv As New List(Of CItem)

これを 2 つの ListBox で実行したいと思います。1 つはインベントリ、もう 1 つはアイテム リストです。これどうやってするの。

クラスにはいくつかのCItemメンバーがあり、アイテムの名前だけを に表示する必要がありますListBox。私は何時間もこれに取り組んできましたが、何も機能しません。この説明は、私がやろうとしていることで意味がありますか? そうでない場合、誰かが私を助けてくれるように、他に何を説明できますか?

4

2 に答える 2

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 に答える
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 に答える