第一个问题是如何让文本框显示列表框中所选员工的所有工资单信息,我不知道如何显示每小时工资,小时数和工资。第二,我希望计算付费按钮在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;
}
答案 0 :(得分:0)
只需运行此代码即可。我做了一些小的修改(并简化了您的Employee对象以获得更快的结果)。
我更改了将项目分配到列表框的方式,并将DataSource
与DisplayMember
一起使用。 DataSource
自动将您的集合绑定到列表框,DisplayMember
告诉它使用哪个属性进行显示。这也意味着SelectedItem
实际上是您可以使用的Employee
对象。
此外,我更改了您处理活动中所选项目的方式。没有必要直接使用数组和索引,它只会让人感到困惑。您可以使用事件的sender
参数来了解触发列表框更改事件的人员。
// create list
private List<Employee> payroll;
public Form1()
{
InitializeComponent();
payroll = new List<Employee>();
// Create some employee objects
payroll.Add(new Employee(1, "H. Potter", "Privet Drive", "201-9090"));
payroll.Add(new Employee(2, "A. Dumbledore", "Hogewarts", "803-1230"));
payroll.Add(new Employee(3, "R. Weasley", "The Burrow", "892-2000"));
payroll.Add(new Employee(4, "R. Hagrid", "Hogwarts", "910-8765"));
employeeListBox.DataSource = payroll;
employeeListBox.DisplayMember = "Name";
}
private void employeeListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Employee selectedEmployee = ((sender as ListBox).SelectedItem as Employee);
txtAddress.Text = selectedEmployee.Address;
txtName.Text = selectedEmployee.Name;
txtPhone.Text = selectedEmployee.PhoneNum;
}
private void btnCalc_Click(object sender, EventArgs e)
{
Employee selectedEmployee = (employeeListBox.SelectedItem as Employee);
// Do calculations here...
}