无法从ListOf传输项目

时间:2013-09-27 11:11:17

标签: vb.net visual-studio listbox

我正在制作一个程序,允许用户将表添加到列表中。 但是我无法将列表显示在listBox上

该表及其属性被添加到类中的原始列表中,但是一旦我尝试显示它,数据似乎就消失了。 我假设我的列表出了问题,但我无法弄清楚是什么。 我现在尝试将列表放在另一个数组列表中,但没有结果。

Imports Table_Tracker_Library
Public Class Main_Form
    Private Property _Table As New Table
    Private Property _ShowList As Table()

Private Sub Btn_Add_Table_Click(sender As Object, e As EventArgs) Handles Btn_Add_Table.Click
    Form_Create_Button.Show()
End Sub



Public Sub Renew()
    _ShowList = _Table.GetList()
    Me.LB_ActiveTables.ClearSelected()
    For Each table As Table In _ShowList
        LB_ActiveTables.Items.Add("Table #" & table.Number)
    Next
    LB_ActiveTables.Refresh()
End Sub

'Private Function GetList() As Table()
'    Dim TempList As New List(Of Table)
'    For Each table As Table In _Table.ListOfTable
'        TempList.Add(table)
'    Next
'    Return TempList.ToArray
'End Function
End Class

-

Imports Table_Tracker_Library
Public Class Form_Create_Button
    Private Property _Table As New Table

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_addtable.Click
        _Table.CreateTable(Me.txb_TableNr.Text, Me.txb_PplNr.Text)
        Main_Form.Renew()
        Me.Close()
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_discard.Click
        Me.Close()
        End Sub
End Class

-

Public Class Table
    Public Property Number As Int32
    Public Property NumberOfPeople As Int32
    Public Property ListOfTable As New List(Of Table)



    Public Sub CreateTable(TableNrString As String, PersonNrString As String)
        Dim TableNR As Int32 = CInt(TableNrString)
        Dim PersonNr As Int32 = CInt(PersonNrString)
        Dim NewTable As New Table With {.Number = TableNR, .NumberOfPeople = PersonNr}
        AddTable(NewTable)
    End Sub

    Private Sub AddTable(Table As Table)
        ListOfTable.Add(Table)
    End Sub

    Public Function GetList() As Table()
        Dim TempList As New List(Of Table)
        For Each table As Table In ListOfTable
            TempList.Add(table)
        Next
        Return TempList.ToArray
    End Function

End Class

1 个答案:

答案 0 :(得分:1)

将主要表单中的private更改为public

Public Class Main_Form
    Public Property Table As New Table
    Private Property _ShowList As Table()

.....

并在辅助表单上使用主表单的表属性

Public Class Form_Create_Button


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_addtable.Click
        If IsNumeric(txb_TableNr.Text) = True AndAlso IsNumeric(txb_PplNr.Text) Then
            If Main_Form.Table.TableIsAvailable(txb_TableNr.Text, txb_PplNr.Text) = True Then
                Main_Form.Table.CreateTable(Me.txb_TableNr.Text, Me.txb_PplNr.Text)
                Main_Form.RenewListBox()
                Me.Close()
            Else
                MessageBox.Show("Table already taken!", "Invalid Table", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
            End If
        Else
            MessageBox.Show("Invalid table number or amount of people", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End If
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btn_discard.Click
        Me.Close()
    End Sub



End Class