如何比较/验证2个不同形式的datetimePickers?例如,我有单独的签入/签出表单,我想验证用户在签入之前没有签出。
how do I compare/validate 2 datetimePickers that are in different forms? eg.I have separate check in/out forms ,and I want to validate that a user not check out before checking in.
推荐答案checkout co= new checkout(datepicker); co.ShowDialog(this);
public CheckOut(string k) { InitializeComponent(); lbldate.Text= k; }
两种形式都是用户界面组件。他们不应该存储信息,只是想象(包括改变的可能性)。因此,两种形式都应该针对相同的数据集。然后,值范围检查属于数据模型部分中的setter: Both forms are User Interface components. They shouldn't store information, just visualize (including the possibility to change) it. So both forms should target the same data set. Value range checks then belong to setters in the data model part: public class SomeTimeRelatedStuff { private DateTime _checkInTime; private DateTime _checkOutTime; public DateTime CheckInTime { get { return(_checkInTime); } set { _checkInTime = value; } } public DateTime CheckOutTime { get { return(_checkOutTime); } } public bool SetCheckOutTime(DateTime checkOutTime) { if(checkOutTime < _checkInTime) { return(false); } _checkOutTime = checkOutTime; return(true); } }
在您的UI代码中,然后使用用户输入的值调用 SetCheckOutTime()。返回值告诉您该值是否已被接受。 很好的部分是检查是在数据类中完成的。那个班级应该知道它的数据以及它必须与其几个部分之间的关系。这有助于保持UI代码清洁业务逻辑。
In your UI code, you then call SetCheckOutTime() with the value that user has entered. The return value tells you if that value has been accepted. The nice part is that the checking is done in the data class. That class should know about its data and the relations that it has to have to its several parts. That helps keeping UI code clean of business logic.