0

第一个问题是如何让文本框显示列表框中所选员工的所有工资单信息,我不知道如何显示 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;




    }
4

1 回答 1

0

只需运行此代码,它就可以工作。我做了一些细微的修改(并简化了您的 Employee 对象以获得更快的结果)。

我更改了您将项目分配给列表框的方式并DataSource改为使用DisplayMemberDataSource自动将您的集合绑定到列表框,并DisplayMember告诉它使用哪个属性进行显示。这也意味着 theSelectedItem实际上是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...
    }
于 2012-09-24T05:08:00.413 回答