将对象存储到一个类中根据请求检索 [英] Storing Objects into a Class & Retrieving upon Request

查看:19
本文介绍了将对象存储到一个类中根据请求检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I seriously need help with my project. I am trying to store specific jobs into a Class, which then displays in a List Box. When selecting the List Box, I want the rest of the information to be displayed into a Text Box.

我可以将作业添加到列表框中,报告按钮按最早到最新对作业进行排序.我似乎无法对显示按钮进行编码以检索其余信息.

I can add Jobs into the List Box, and the Report button sorts the Job by Earliest to Latest. I just CANNOT seem to code the Display Button to retrieve the rest of the information.

http://i.stack.imgur.com/0eV5j.png

我做错了什么?

我的代码:

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

推荐答案

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

The bottom line is that you stored string values to the LBs:

    str = person.Name & ",  " & person.Deadline.ToString 
    lsbReport.Items.Add(str)

因此,这就是您返回的结果,这使得将创建的字符串与其代表的对象连接起来很困难.

So, that is what you get back making it difficult to connect the string created with the object it represents.

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

Listboxes and comboboxes can store objects as well as strings. Simple Demo:

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

将对象存储到列表框很简单:

storing an object to a listbox is simple:

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

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

<小时>

一旦你有这些东西的课程,你就有很多选择.您可以将它们存储到可以与列表或组合框一起使用的 List(Of 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.SelectedItem will now be an Employee object, which should make displaying details elsewhere simple.

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

To make it even more efficent, you can use the list as a DataSource so you dont have to add references to the listbox:

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

这篇关于将对象存储到一个类中根据请求检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆