2014-10-19 96 views
1

我的按鈕btnAction總是在第一個組合框cb1 textchanged事件後消失並且從不出現。當emp和tmp不同時,我想要這個按鈕可見,而當它們不是時不可見。基於選定員工的更改按鈕可見性

public class Employee 
{ 
    [Key] 
    public int EmployeeId { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int Phone { get; set; } 
    public int? SalaryId { get; set; } 
    public virtual Salary Salary { get; set; } 

    public static bool operator ==(Employee le, Employee re) 
    { 
     if (le.FirstName != re.FirstName) 
      return false; 
     if (le.LastName != re.LastName) 
      return false; 
     if (le.Phone != re.Phone) 
      return false; 
     if (le.SalaryId != re.SalaryId) 
      return false; 
     if (le.Salary != re.Salary) 
      return false; 
     return true; 
    } 
    public static bool operator !=(Employee le, Employee re) 
    { 
     if (le.FirstName == re.FirstName) 
      return false; 
     if (le.LastName == re.LastName) 
      return false; 
     if (le.Phone == re.Phone) 
      return false; 
     if (le.SalaryId == re.SalaryId) 
      return false; 
     if (le.Salary == re.Salary) 
      return false; 
     return true; 
    } 

} 

public class Salary 
{ 
    [Key] 
    public int SalaryId { get; set; } 
    public string Name { get; set; } 
    public decimal? Amount { get; set; } 
    public static bool operator ==(Salary ls, Salary rs) 
    { 
     if (!ls.Name.Equals(rs.Name)) 
      return false; 
     if (ls.Amount != rs.Amount) 
      return false; 
     return true; 
    } 
    public static bool operator !=(Salary ls, Salary rs) 
    { 
     if (ls.Name.Equals(rs.Name)) 
      return false; 
     if (ls.Amount == rs.Amount) 
      return false; 
     return true; 
    } 
} 

和方法時,組合框CB1改變

public void cb1_OnTextUpdate(object sender, EventArgs e) 
{ 
    if(dataType == 1) 
    { 
     Employee tmp; 
     tmp = emp/*es*/; 
     tmp.FirstName = cb1.Text; 
     if (tmp == emp/*es*/) 
      this.btnAction.Visible = false; 
     if (tmp != emp/*es*/) 
      this.btnAction.Visible = true; 
    } 
} 

回答

1

使用本

public void cb1_OnTextUpdate(object sender, EventArgs e) 
{ 
    if(dataType == 1) 
    { 
     this.btnAction.Visible = !(cb1.Text == emp.FirstName) 
    } 
} 

但我認爲你應該修改你的!=運營商。假設兩名僱員(emp1,emp2)具有相同的名字。

均爲emp1 == emp2,emp1 != emp2爲假!

0

你永遠不會創建一個新員工時調用。

tmp = emp/*es*/; 

這條線給你叫tmp引用同一名員工emp另一個變量。

tmp.FirstName = cb1.Text; 

此行更改了您曾經擁有的唯一的員工名。所以最後,你正在比較emp與自己。如果你的操作符正確實現,那應該總是正確的。

0

兩者都是相同的。因爲tempemp都指向相同的對象,所以如果它們中的任何一個對其值進行了任何更改。兩者都會有這些變化。

你應該做這樣的事情:

if(cb1.Text == emp.FirstName) 
    this.btnAction.Visible = false; 
else 
    this.btnAction.Visible = true; 
相關問題