第一个问题是如何让文本框显示列表框中所选员工的所有工资单信息,我不知道如何显示 HourlyWage、Hours 和 Salary。其次,我希望计算薪酬按钮在 ListBox 控件中显示当前选定员工的薪水。我不确定我的代码有什么问题以及为什么它没有显示。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
// create list
private List<Employee> payroll;
private int count = 0;
private int SIZE = 0;
public Form1()
{
InitializeComponent();
payroll = new List<Employee>(SIZE);
// Create some employee objects
payroll.Add(new Hourly(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00));
payroll.Add(new Salaried(2, "A. Dumbledore", "Hogewarts", "803-1230", 1200));
payroll.Add(new Hourly(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00));
payroll.Add(new Salaried(4, "R. Hagrid", "Hogwarts", "910-8765", 1000));
foreach (Employee emp in payroll)
employeeListBox.Items.Add(emp.Name);
}
private void buttonCalcPay_Click(object sender, EventArgs e)
{
textBoxCheck.Clear();
int index = count;
if (index < SIZE)
{
//The Compute Pay Button: When this button is clicked, display a
//paycheck for the currently selected employee in the ListBox
//control.
string ostring = ("Fluffshuffle Electronics check no.");
ostring += string.Format("{0}", index);
ostring += Environment.NewLine;
ostring += Environment.NewLine;
ostring += " pay to the order of";
ostring += payroll[index].Name;
ostring += Environment.NewLine;
ostring += " ";
ostring += string.Format("{0:C}", payroll[index].CalcPay());
ostring += Environment.NewLine;
ostring += Environment.NewLine;
ostring += " First National Bank";
textBoxCheck.Text = ostring;
textBoxName.Text = payroll[index].Name;
textBoxAddress.Text = payroll[index].Address;
textBoxPhone.Text = payroll[index].PhoneNum;
textBoxEmpNum.Text = string.Format("{0}", payroll[index].EmpNum);
//index++;
//see if object is hourly
Hourly someEmp1 = payroll[index] as Hourly;
if (someEmp1 != null)
{
textBoxHours.Text = string.Format("{0:F2}", someEmp1.HoursWorked);
textBoxHourlyWage.Text = string.Format("{0:F2}", someEmp1.HourlyWage);
textBoxSalary.Clear();
}
//not hourly, must be salary
Salaried someEmp2 = payroll[index] as Salaried;
if (someEmp2 != null)
{
textBoxHours.Clear();
textBoxHourlyWage.Clear();
textBoxSalary.Text = string.Format("{0:F2}", someEmp2.Salary);
}
else
{
buttonCalcPay.Enabled = false;
textBoxName.Clear();
textBoxAddress.Clear();
textBoxEmpNum.Clear();
textBoxPhone.Clear();
textBoxHours.Clear();
textBoxHourlyWage.Clear();
textBoxSalary.Clear();
count = 0;
}
} count++;
}
private void textBoxCheck_TextChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBoxName.Text = payroll[employeeListBox.SelectedIndex].Name;
textBoxAddress.Text = payroll[employeeListBox.SelectedIndex].Address;
textBoxEmpNum.Text = string.Format("(0)",payroll[employeeListBox.SelectedIndex].EmpNum);
textBoxPhone.Text = payroll[employeeListBox.SelectedIndex].PhoneNum;
}