【问题标题】:Adding Selected Item in a Listbox to a Label in Visual Basic将列表框中的选定项添加到 Visual Basic 中的标签
【发布时间】:2014-04-24 03:44:33
【问题描述】:

在我正在编写的程序中,我被困在最后一个步骤中,即显示从标签列表框中选择的员工姓名和薪水(一个用于姓名,一个用于薪水),但我不能弄清楚如何做到这一点。员工的姓名和薪水从文件中读入,并以 3 种不同的形式放置在相应的列表框中(两种仅列出姓名,另一种仅列出薪水)。在最后一个表单中,用户应该选择其中一名员工的姓名,并且该人的姓名应该出现在一个标签 (lblName) 中,他们的薪水应该出现在另一个标签 (lblSalary) 中。这些名称列在可选列表框中,但是当我单击其中一个时,没有任何反应。

这是我目前在主窗体上的代码:

Option Strict On
Imports System.IO

Public Class Main

Private Sub open_Click(sender As Object, e As EventArgs) Handles open.Click
    Dim open As New OpenFileDialog
    open.Filter = "text files |*.txt|All Files|*.*"
    open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

    If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim selectedFileName As String = System.IO.Path.GetFileName(open.FileName)
        showNames.Enabled = True
        showSalaries.Enabled = True
        showEmployee.Enabled = True
    End If

    Dim container As New List(Of Project9)
    Using reader As New StreamReader(open.OpenFile)
        While Not reader.EndOfStream
            Dim employee As New Project9
            employee.Name = reader.ReadLine()
            employee.Salary = CDbl(reader.ReadLine())
            container.Add(employee)
        End While
    End Using

    For Each item As Project9 In container
        Names.lstNames.Items.Add(item.Name)
        frmTotal.lstShow.Items.Add(item.Name)
        Salaries.lstSalaries.Items.Add(item.Salary)
    Next

End Sub

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
    Names.Close()
    Salaries.Close()
    frmTotal.Close()
End Sub

Private Sub showNames_Click(sender As Object, e As EventArgs) Handles showNames.Click
    Names.Show()
End Sub

Private Sub showSalaries_Click(sender As Object, e As EventArgs) Handles showSalaries.Click
    Salaries.Show()
End Sub

Private Sub showEmployee_Click(sender As Object, e As EventArgs) Handles showEmployee.Click
    frmTotal.Show()
End Sub
End Class

Public Class Project9

Dim strName As String
Dim dblSalary As Double

Public Property Name As String
    Get
        Return strName
    End Get
    Set(value As String)
        strName = value
    End Set
End Property

Public Property Salary As Double
    Get
        Return dblSalary
    End Get
    Set(value As Double)
        If dblSalary < 0 Then
            dblSalary = 10
        End If
        dblSalary = value
    End Set

End Property


Public Function computeSalary(intMonths As Integer) As Double
    Dim dblTotal As Double = dblSalary * intMonths

    Return dblTotal
End Function

End Class

这是第 4 种形式的代码,它是在标签中显示所选项目的代码:

Public Class frmTotal

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

    lblName.Text = lstShow.SelectedItem(Name)

    Dim intMonths As Integer
    intMonths = InputBox("How many months would you like to calculate this employee's salary for?")

End Sub
End Class

另外,我如何才能使按钮仅在选择项目后才可见?

任何帮助将不胜感激。

【问题讨论】:

    标签: vb.net listbox


    【解决方案1】:

    如果您利用列表框的 Datasource 属性,您实际上可以用 Project9 项填充列表框,并使用 DisplayMember 属性来选择您在列表框中显示的属性。这有几个优点。 Whenever a listbox item is selected it can be cast as an object of type Project9 and you can display whichever properties you want to the labels.此外,选择在列表框中绑定在一起,以便一个选择与其他选择相同。下面是一个示例:

    Dim employees As New List(Of Project9)(
        {
            New Project9 With {.Name = "A", .Salary = 1000},
            New Project9 With {.Name = "B", .Salary = 1200},
            New Project9 With {.Name = "C", .Salary = 1100}
            })
    ListBox1.DataSource = employees
    ListBox1.DisplayMember = "Name"
    ListBox2.DataSource = employees
    ListBox2.DisplayMember = "Salary"
    
    
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim selectedemployee = DirectCast(ListBox1.SelectedItem, Project9)
        Label1.Text = selectedemployee.Name
        Label2.Text = selectedemployee.Salary.ToString
    End Sub
    

    【讨论】:

    • 唯一的问题是姓名和薪水位于一个文件中,而我用于创建程序的文件与用于评分的文件不同,尽管该文件将与我的布局相同。
    • 如何加载列表无关紧要。一旦你有了一个列表,这将向你展示如何在列表框中显示你需要的值,以及如何检索选定的值并将这些值放入标签中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2022-01-04
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多