从日期到日期使用c#Datetimepicker验证进行验证
private void dateTimePickerHolidayToDate_ValueChanged(object sender, EventArgs e)
{
DateTime fromdate = Convert.ToDateTime(dateTimePickerFromDate.Text);
DateTime todate1 = Convert.ToDateTime(dateTimePickerToDate.Text);
if (fromdate1 <= todate1)
{
TimeSpan daycount = todate1.Subtract(fromdate1);
int dacount1 = Convert.ToInt32(daycount.Days) + 1;
txtDay.Text = Convert.ToString(dacount1);
}
else
{
MessageBox.Show("From Date Must be Less Than To Date");
}
}
HI祝每个人下午好,在Windows应用程序我使用两个datetimepicker从日期和到日期...选择到日期后,我需要计算从日期和到日期天数之间的天数,为此我使用上面的代码...但是当我将to datetimepicker移动到小于from datetimepicker时,它执行无限次显示消息框。如何只显示一次该消息框...
HI frnds Good Afternoon to every one,In windows application i am using two datetimepicker for "from date" and "to date" ...After choosing "to date" i need to count number of days in between "from date" and "to date" days count ,for that i used above code...but when move my "to datetimepicker" less than "from datetimepicker" it executing infinite times of showing message box.How to show only one time that message box...
而不是dateTimePicker2_ValueChanged
使用dateTimePicker2_CloseUp
事件,所以你的最终代码将是
Instead ofdateTimePicker2_ValueChanged
UsedateTimePicker2_CloseUp
event , so your final code will be
private void dateTimePicker2_CloseUp(object sender, EventArgs e)
{
DateTime fromdate = Convert.ToDateTime(dateTimePicker1.Text);
DateTime todate1 = Convert.ToDateTime(dateTimePicker2.Text);
if (fromdate <= todate1)
{
TimeSpan daycount = todate1.Subtract(fromdate);
int dacount1 = Convert.ToInt32(daycount.Days) + 1;
MessageBox.Show(Convert.ToString(dacount1));
}
else
{
MessageBox.Show("From Date Must be Less Than To Date");
}
}
CloseUp
仅当用户最终选择值时才触发事件。 ValueChanged
当您更改月份时也会触发事件。
我已更正您的变量名称在代码:)运行在我的本地方框
标记为解决方案如果这解决了你的问题
谢谢......
CloseUp
event is triggered only when the user finally selects a value. ValueChanged
event will fire when you change the month also that was your issue.
I have corrected your variable names in code :) to run in my local box
mark it as solution if this resolved your issue
Thanks...
DateTime selectedDate = Convert.ToDateTime(dtpExpireDate.Value);
DateTime todayDate = Convert.ToDateTime(DateTime.Now);
if (selectedDate < todayDate)
{
MessageBox.Show("Selected date Must be greater then Today's date");
}
我认为在来自fromDate的ClosUp事件中你可以配置类似的东西:
I think that in ClosUp event from "fromDate" you could configure something like:
dateTimePicker2.MinDate = dateTimePicker1.Value
这样,用户只能选择数据等于或在第一个日期之后。
This way, user can only choose a data equal or after the first date.