如何验证日期时间选择器控件 [英] How to validate datetime Picker control
问题描述
大家好,有人可以推荐我..
在我的Windows窗体中我有两个datetimepicker控件
从日期 - > dtpFromMonth
到日期---> dtpToMonth
我的Scenrio是如果用户选择5月1日他能够选择日期的起始日期不应超过31天
。
string Fromdate = Convert.ToString(dtpFromMonth .Value);
string V1 = Convert.ToString(dtpFromMonth.Value.AddDays(31));
string V2 = Convert.ToString(dtpToMonth.Value);
if(v1< v2)---->在这里我得到转换错误字符串到int .. Ya ofcourse语句是int。
{
消息(请选择仅一个月);
}
预期结果:
起始日期 - > 2013年5月1日
到目前为止 - > 2013年6月5日
留言应该来
请选择一个月
问候,
Vino
Hi guys anyone can suggest me..
In my windows form i have two datetimepicker control
From Date-->dtpFromMonth
To Date--->dtpToMonth
My Scenrio is If User selects 1st May in the From Date he can able to choose to date should not be
greater than 31 days.
string Fromdate = Convert.ToString(dtpFromMonth.Value);
string V1 = Convert.ToString(dtpFromMonth.Value.AddDays(31));
string V2 = Convert.ToString(dtpToMonth.Value);
if(v1<v2)----> Here i am getting convertion error string to int.. Ya ofcourse statement is int.
{
Message("Please Select One Month Only");
}
Expected result:
From Date--> 1st May 2013
To Date--> 5th June 2013
Message should come
"Please Select One Month Only"
Regards,
Vino
推荐答案
DateTime date;
private void ToDate_ValueChanged(object sender, EventArgs e)
{
date = dateTimePicker1.Value.AddMonths(1);
if (dateTimePicker2.Value > date)
MessageBox.Show("'To Date' cannot be greater than 1 month from 'From Date'");
}
不要使用表示时间的字符串,与时间本身一起工作:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ]!
-SA
Don''t work with string representing time, work with time itself:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^]!
—SA
DateTime Fromdate = Convert.ToDateTime(dtpFromMonth.Value);
DateTime V1 = Convert.ToDateTime(dtpFromMonth.Value.AddDays(31));
DateTime V2 = Convert.ToDateTime(dtpToMonth.Value);
if(V1< V2)
{
obal.Message_OK_Information(请选择仅一个月);
dtpToMonth.Focus();
}
DateTime Fromdate = Convert.ToDateTime(dtpFromMonth.Value);
DateTime V1 = Convert.ToDateTime(dtpFromMonth.Value.AddDays(31));
DateTime V2 = Convert.ToDateTime(dtpToMonth.Value);
if (V1 < V2)
{
obal.Message_OK_Information("Please Select One Month Only");
dtpToMonth.Focus();
}
这篇关于如何验证日期时间选择器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!