2014-04-24 53 views
0

在我正在編寫的程序中,我卡住了最後一個步驟,它顯示從標籤中的列表框中選擇的員工姓名和工資名字和薪水),但我無法弄清楚如何做到這一點。員工的姓名和工資從文件中讀入,並放在3個不同表格(兩個只列出名字,一個只列出薪水)的相應列表框中。在最後一種形式中,用戶應該選擇其中一名僱員的姓名,並且該人的姓名應該出現在一個標籤(lblName)中,並且他們的薪水應該出現在另一個標籤(lblSalary)中。名稱列在可選列表框中,但是當我點擊其中一個時,什麼也沒有發生。將列表框中的選定項目添加到Visual Basic中的標籤

這裏是我迄今爲止在主窗體上的代碼:

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 

另外,如何才能在按鈕被選中後才能看到按鈕?

任何幫助將不勝感激。

回答

0

如果您利用列表框的數據源屬性,您實際上可以使用Project9項填充列表框,並使用DisplayMember屬性來選擇顯示在列表框中的屬性。這有幾個優點。只要選擇了列表框項目,就可以將其轉換爲Project9類型的對象,並且可以向標籤顯示所需的任何屬性。此外,選擇是通過列表框連接在一起的,因此一個選擇在其他選擇中是相同的選擇。下面是一個例子:

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 
+0

唯一的問題是名稱和工資位於文件中,而我用來創建程序的文件與將要使用的文件不同對它進行分級,儘管該文件的佈局與我的相同。 –

+0

如何加載列表無關緊要。一旦你有了一個列表,它將向你展示如何在列表框中顯示你需要的值,以及如何檢索選中的值並將這些值放入標籤中。 – tinstaafl

相關問題