Question

In the program I'm writing I'm stuck on one of the last steps which is to display an employee's name and salary selected from a listbox in labels (one for the name and one for the salary), but I cannot figure out how to do this. The employee's names and salaries are read in from a file and are placed in the corresponding listboxes on 3 different forms (two that only list names, and one that only lists salaries). On the last form, the user is supposed to select the name of one of the employees and that person's name is supposed to appear in one label (lblName) and their salary is supposed to appear in another label (lblSalary). The names are listed in the selectable listbox, but when I click on one of them nothing happens.

Here is the code I have so far on the main form:

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

And here's the code from the 4th form which is the one displaying the selected item in labels:

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

Also, how would I be able to make a button visible only after an item has been selected?

Any help would be greatly appreciated.

Was it helpful?

Solution

If you leverage the Datasource property of the listbox you can actually fill the listbox with Project9 items and use the DisplayMember property to choose which property you display in the listbox. This has several advantages. 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. Also the selection is tied together across the listboxes so that a selection in one is the same selection in the others. Here's an example of how that might look:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top