2017-06-03 28 views
-4

我有兩種形式:薪水員工和小時工,員工詳細信息從文本文件加載。在我的主窗體中,有一個帶有員工姓名的列表框,一旦點擊了一個列表框,我希望能夠按下我的主窗體上的編輯員工詳細信息按鈕,並找到正確的窗體,我正在努力如何去做這個。我的主要形式的代碼是在這裏:c#獲取正確的表單以顯示

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; 
    } 
} 

從負載法的員工類代碼:

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(); 
    } 
} 

and lastly my sa laried員工形式:

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(); 
} 

任何幫助這個問題將是偉大的!

+0

究竟是什麼問題? – stybl

+3

重複https://stackoverflow.com/questions/44345739/c-sharp-showing-the-right-form(現在已被作者刪除)來自另一個用戶。集體功課? – ASh

+0

@Sty id喜歡能夠點擊編輯按鈕,同時在列表框中選擇一個名稱,右邊的​​窗體在 – rosie

回答

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方法返回的對象類型的名稱。通過覆蓋在公共基類Employee中從object繼承的ToString方法來啓用列表框以正確顯示員工。

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

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

嗨,謝謝你的回覆。我試過這個,但是我發現HourlyEmployee和SalariedEmployee上的錯誤「類型employeeclass的表達式不能由SalariedEmployee/HourlyEmployee類型的模式處理。通過普通基類,你是指擁有第一個名字的僱員類姓氏地址等? – rosie

+0

可能您的'SalariedEmployee'和'HourlyEmployee'類不像我期望的那樣從'EmployeeClass'派生,因此只需將所選僱員輸入爲'object'(在這種情況下不需要轉換)。 –

+0

將員工類別更改爲Employee,但是從這兩行中獲取錯誤:var frm = new Hourly_Employee(); frm。employeeEntry = hEmployee;首先是「frm」在這個範圍內已經定義了一個名爲frm的局部變量或函數。然後在第二行「frm」有錯誤「使用未分配的本地變量。最後一個錯誤是在hEmployee」不能隱式轉換類型HRApplication.HourlyEmployee到HRApplication.SalariedEmployee – rosie

相關問題