簡體   English   中英

為什么不能正確顯示listBox1_SelectedIndexChanged

[英]why wont this display correctly listBox1_SelectedIndexChanged

第一個問題是如何獲取文本框以顯示列表框中所選雇員的所有薪資信息,我不確定如何顯示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;




    }

只需運行這段代碼,它就可以工作。 我做了一些細微的修改(並簡化了Employee對象以獲得更快的結果)。

我更改了將項目分配給列表框的方式,並將DataSourceDisplayMember使用。 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...
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM