Vb.net 将对象存储到类&;根据要求检索

Vb.net 将对象存储到类&;根据要求检索,vb.net,Vb.net,我的项目真的需要帮助。 我试图将特定作业存储到一个类中,然后该类将显示在一个列表框中。 选择列表框时,我希望其余信息显示在文本框中 我可以将作业添加到列表框中,并且“报告”按钮按从早到晚对作业进行排序。 我似乎无法对显示按钮进行编码以检索其余信息 我做错了什么 我的代码: Public Class Form1 Dim jobList As List(Of UserInformation) = New List(Of UserInformation) Dim j As UserInformat

我的项目真的需要帮助。 我试图将特定作业存储到一个类中,然后该类将显示在一个列表框中。 选择列表框时,我希望其余信息显示在文本框中

我可以将作业添加到列表框中,并且“报告”按钮按从早到晚对作业进行排序。 我似乎无法对显示按钮进行编码以检索其余信息

我做错了什么

我的代码:

Public Class Form1

Dim jobList As List(Of UserInformation) = New List(Of UserInformation)
Dim j As UserInformation = New UserInformation()
Private Sub btnReport_Click(sender As Object, e As EventArgs) Handles btnReport.Click

    Dim p As UserInformation = New UserInformation()
    Dim qty As Integer = jobList.Count - 1

    Dim name(qty) As String

    Dim deadline(qty) As Date

    Dim i As Integer = 0

    'fill the array

    For i = 0 To qty

        p = jobList(i)

        name(i) = p.Name

        deadline(i) = p.Deadline

    Next

    'sort the array

    Dim done As Boolean = False

    While done = False

        done = True

        For i = 0 To qty - 1

            Dim tempName As String

            Dim tempDate As Date

            If deadline(i) > deadline(i + 1) Then

                tempName = name(i)

                tempDate = deadline(i)

                name(i) = name(i + 1)

                deadline(i) = deadline(i + 1)

                name(i + 1) = tempName

                deadline(i + 1) = tempDate

                done = False

            End If

        Next

    End While

    lsbReport.Items.Clear()

    lblListbox.Text = "List in date order"

    For i = 0 To name.Length - 1

        Dim str As String

        str = name(i) + ",  "

        str += deadline(i).ToString + "."

        lsbReport.Items.Add(str)

    Next

End Sub

Private Sub updateListBox()

    lsbReport.Items.Clear()

    lblListbox.Text = "All people in the List"

    For Each person As UserInformation In jobList

        Dim str As String

        str = person.Name + ",  "

        str += person.Deadline.ToString + "."

        lsbReport.Items.Add(str)

    Next

End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim p As UserInformation = New UserInformation()

    p.Name = firstNameText.Text

    p.Deadline = lastNameText.Value

    jobList.Add(p)

    updateListBox()

End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    Dim job_info As UserInformation = CType(lsbReport.SelectedItem(), UserInformation)
    txtReport.Text = "Job Title: " & job_info.Name() & Environment.NewLine
    txtReport.Text &="Job DeadLine: " & job_info.Deadline & Environment.NewLine
    txtReport.Text &="Job Description" & job_info.Description

End Sub
End Class




Public Class UserInformation
Public job_deadline As Date
Public job_name As String
Public job_description As String
Public Property Name() As String
    Get
        Return job_name
    End Get
    Set(ByVal value As String)
        job_name = value
    End Set
End Property
Public Property Deadline() As String
    Get
        Return job_deadline
    End Get
    Set(ByVal value As String)
        job_deadline = value
    End Set
End Property

Public Property Description() As String
    Get
        Return job_description
    End Get
    Set(ByVal value As String)
        job_description = value
    End Set
End Property
End Class

底线是您将字符串值存储到LBs:

    str = person.Name & ",  " & person.Deadline.ToString 
    lsbReport.Items.Add(str)
因此,这就是您得到的结果,使得很难将创建的字符串与其表示的对象连接起来

列表框和组合框可以存储对象和字符串。简单演示:

Public Class Employee
    Public Property ID As Integer      ' a db ID maybe
    Public Property Name As String
    Public Property Department As String

    Public Property HireDate As Date

    Public Overrides Function ToString As String
        Return Name                 ' text to show in controls

        ' in a more realistic class it might be:
        ' Return String.Format("{0}, {1} ({2})", LastName,
        '                 FirstName, BadgeNumber)
        ' e.g. "Whitman, Ziggy (123450)"
    End Function
End Class

Friend EmpList As New List(of Employee)      ' if needed
将对象存储到列表框很简单:

Dim newEmp As Employee
newEmp.Name = "Ziggy"      
' set props as needed

myLB.Items.Add(newEmp)         ' add to a ListBox directly.

一旦你有了这些东西的课程,你就有了很多选择。您可以将它们存储到列表(共T个),该列表可与列表或组合框一起使用:

Private EmpList As New List(Of Employee)
...
EmpList.Add(newEmp)             ' adding to a list is same as a ListBox 

' add from List to a control:

myLB.Items.AddRange(Emps.ToArray)
myLB.SelectedItem现在将成为Employee对象,这将使在别处显示详细信息变得简单

为了提高效率,您可以将列表用作数据源,这样就不必向列表框添加引用:

myLB.DataSource = EmpList
myLB.DisplayMember = "Name"      ' the property to display
myLB.ValueMember = "Id"          ' what to use for SelectedValue

你的意思可能是
.Text&=“作业截止日期:…
而不是
.Text=+”作业死线…
。对吧?谢谢你的帮助,修好了。但仍然不起作用;[这意味着…?是否有编译器错误?异常?意外结果?我得到:“类型为'System.InvalidCastException'的未处理异常……其他信息:无法将类型为'System.String'的对象强制转换为类型”您的列表框中有字符串。您试图将它们强制转换为
UserInformation
。当然,这不起作用。请将
UserInformation
放入列表框,并为输出应用适当的格式。