【发布时间】: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
另外,我如何才能使按钮仅在选择项目后才可见?
任何帮助将不胜感激。
【问题讨论】: