1

我正在制作一个允许用户将表格添加到列表的程序。但我无法让列表显示在 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
4

1 回答 1

1

将主要形式更改privatepublic

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

.....

并在辅助窗体上使用主窗体的 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
于 2013-10-25T15:48:05.927 回答