c#获取正确的表单以显示

时间:2017-06-03 17:40:55

标签: c# winforms

我有两个表格是受薪雇员和每小时雇员,其中有员工详细信息从文本文件加载。在我的主要表单中有一个列出了员工姓名的列表框,一旦点击一个,我希望能够按下主表单上的编辑员工详细信息按钮,并找到正确的表单,我正在努力去做这个。我的主要表格的代码在这里:

 public partial class MainForm : Form
 {
// The file used to store employee details
string employeesFile = "employees.txt";


// The collection used to hold the employee data
Employees employees;


public MainForm()
{
    InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)
{
    employees = new Employees();
    if (!employees.Load(employeesFile))
    {
        MessageBox.Show("Unable to load employees file");
    }
    else
    {
        PopulateListBox();
    }
}

private void PopulateListBox()
{
    listBoxEmployees.Items.Clear();
    foreach (Employee employee in employees)
    {
        listBoxEmployees.Items.Add(employee.LastName + "," + 
employee.FirstName);
    }
    listBoxEmployees.SelectedIndex = 0;
}

private void listBoxEmployees_DoubleClick(object sender, EventArgs e)
{ }

private void buttonEdit_Click(object sender, EventArgs e)
{

}

我的加载方法:

{
public bool Load(string employeesFile)
{
    bool status = false;
    StreamReader inputFile = null;
    string inputLine;

    if (!File.Exists(employeesFile))
    {
        return false;
    }
    try
    {
        inputFile = new StreamReader(employeesFile);
        if (inputFile != null)
        {
            inputLine = inputFile.ReadLine();
            while (inputLine != null)
            {
                Employee employeeEntry = 
EmployeeClass.NewEmployee(inputLine);
                if (employeeEntry != null)
                {
                    this.Add(employeeEntry);
                }
                inputLine = inputFile.ReadLine();
            }
            inputFile.Close();
        }
        status = true;
    }
    catch
    {
    }
    return status;
   }
 }

来自load方法的employees类代码:

 public class EmployeeClass
 {
 public static Employee NewEmployee(string employeeData)
 {
    if (employeeData.Length < 1)
    {
        return null;
    }
    switch (employeeData[0])
    {
        case 'S':
           return new SalariedEmployee(employeeData);

        case 'H':
            return new HourlyEmployee(employeeData);

        default:
            return null;

每小时员工表格:

public partial class Hourly_Employee : Form {

HourlyEmployee _employeeEntry;

public Hourly_Employee()
{
  InitializeComponent();
}


public HourlyEmployee employeeEntry
{
    get
    {
        return _employeeEntry;
    }
    set
    {
        _employeeEntry = value;
    }
}

private void Hourly_Employee_Load(object sender, EventArgs e)
{
    textBoxlastName.Text = _employeeEntry.LastName;
    textBoxfirstName.Text = _employeeEntry.FirstName;
    textBoxaddress.Text = _employeeEntry.Address;
    textBoxpostCode.Text = _employeeEntry.PostCode;
    textBoxphoneNumber.Text = _employeeEntry.PhoneNumber;
    dateTimePickerdateOfBirth.Text = 
_employeeEntry.DateOfBirth.ToString();
    textBoxhourlyPay.Text = _employeeEntry.HourlyPay.ToString();
    textBoxoverTimePay.Text = _employeeEntry.OvertimePay.ToString();
  }
 }

最后我的受薪员工表格:

public partial class Salary_Employee : Form
{
SalariedEmployee _employeeEntry;

public SalariedEmployee employeeEntry
{
    get
    {
        return _employeeEntry;
    }
    set
    {
        _employeeEntry = value;
    }
}

private void Salary_Employee_Load(object sender, EventArgs e)
{
    textBoxlastName.Text = _employeeEntry.LastName;
    textBoxfirstName.Text = _employeeEntry.FirstName;
    textBoxaddress.Text = _employeeEntry.Address;
    textBoxpostCode.Text = _employeeEntry.PostCode;
    textBoxphoneNumber.Text = _employeeEntry.PhoneNumber;
    dateTimePickerdateOfBirth.Text = 
 _employeeEntry.DateOfBirth.ToString();
    textBoxSalary.Text = _employeeEntry.Salary.ToString();
 }

任何有关此问题的帮助都会很棒!!

1 个答案:

答案 0 :(得分:0)

诀窍是将员工对象添加到列表框,而不是仅包含员工姓名的字符串。这允许您直接从列表框中检索所选员工。否则,您需要一种方法来查找属于名称的员工对象。

使用所选项目的类型来确定员工类型和员工表单。

private void buttonEdit_Click(object sender, EventArgs e)
{
    // Get the selected employee from the listBox.
    object employee = listBoxEmployees.SelectedItem;

    if (employee != null) { // An employee has been selected.
         // Use the new C# 7.0 switch syntax in order to switch by employee type.
        switch (employee) {
            case SalariedEmployee sEmployee:
                var sfrm = new Salary_Employee(); // Open the salaried employee form..
                sfrm.employeeEntry = sEmployee;
                sfrm.Show();
                break;
            case HourlyEmployee hEmployee:
                var hfrm = new Hourly_Employee(); // Open the hourly employee form.
                hfrm.employeeEntry = hEmployee;
                hfrm.Show();
                break;
        }
    }
}

如果您使用较旧的C#版本,则可以使用

测试类型
if (employee is SalariedEmployee) {
    var frm = new Salary_Employee();;
    frm.employeeEntry = (SalariedEmployee)employee;
    frm.Show();
} else if (employee is HourlyEmployee) {
    var frm = new Hourly_Employee();
    frm.employeeEntry = (HourlyEmployee)employee;
    frm.Show();
}

默认情况下,对象的ToString方法返回对象类型的名称。通过覆盖公共基类ToString中从object继承的Employee方法,使列表框能够正确显示员工。

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return LastName + ", " + FirstName;
    }
}