基于选定员工的更改按钮可见性
问题描述:
我的按钮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;
}
}
答
使用本
public void cb1_OnTextUpdate(object sender, EventArgs e)
{
if(dataType == 1)
{
this.btnAction.Visible = !(cb1.Text == emp.FirstName)
}
}
但我认为你应该修改你的!=
运营商。假设两名雇员(emp1,emp2)具有相同的名字。
均为emp1 == emp2
,emp1 != emp2
为假!
答
你永远不会创建一个新员工时调用。
tmp = emp/*es*/;
这条线给你叫tmp
引用同一名员工为emp
另一个变量。
tmp.FirstName = cb1.Text;
此行更改了您曾经拥有的唯一的员工名。所以最后,你正在比较emp
与自己。如果你的操作符正确实现,那应该总是正确的。
答
两者都是相同的。因为temp
和emp
都指向相同的对象,所以如果它们中的任何一个对其值进行了任何更改。两者都会有这些变化。
你应该做这样的事情:
if(cb1.Text == emp.FirstName)
this.btnAction.Visible = false;
else
this.btnAction.Visible = true;