[英]Adding constraint in Windows Store app for date
I have made a simple Windows 8.1 store app named Scheduler. 我制作了一个简单的Windows 8.1存储应用程序,名为Scheduler。 It is like a ToDo app where it asks the user to fill in the details of personal or official meeting etc. and will notify the user on that particular day that they have a meeting. 就像ToDo应用一样,它要求用户填写个人会议或正式会议等的详细信息,并会在该特定日期通知用户他们有会议。
I want to add a constraint that the app checks the current date before saving the details and a message box appear user if the schedule is pre-dated. 我想添加一个约束,使应用程序在保存详细信息之前先检查当前日期,如果计划已过时,则会出现一个消息框,提示用户。
How to implement this? 如何实现呢?
public MainPage()
{
this.InitializeComponent();
fillYear(2014, 2114);
fillMonth();
fillDays();
cboxMonth.SelectionChanged += cboxMonth_SelectionChanged;
cboxYear.SelectionChanged += cboxYear_SelectionChanged;
lstViewTaskCategories.Items.Add("Personal");
lstViewTaskCategories.Items.Add("Official");
lstViewTaskCategories.SelectedIndex = 0;
}
void cboxYear_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
fillDays();
}
void cboxMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
fillDays();
}
void fillDays()
{
cboxDay.Items.Clear();
cboxDay.Items.Add("");
try
{
int days = DateTime.DaysInMonth(Convert.ToInt32(cboxYear.SelectedValue), getMonth(cboxMonth.SelectedValue.ToString()));
for (int i = 1; i <= days; i++)
cboxDay.Items.Add(i);
cboxDay.SelectedIndex = 0;
}
catch (Exception ex)
{
}
}
int getMonth(string monthName)
{
switch (monthName)
{
case "January":
return 1;
case "February":
return 2;
case "March":
return 3;
case "April":
return 4;
case "May":
return 5;
case "June":
return 6;
case "July":
return 7;
case "August":
return 8;
case "September":
return 9;
case "October":
return 10;
case "November":
return 11;
case "December":
return 12;
default:
return -1;
}
}
void fillMonth()
{
cboxMonth.Items.Add("");
cboxMonth.Items.Add("January");
cboxMonth.Items.Add("February");
cboxMonth.Items.Add("March");
cboxMonth.Items.Add("April");
cboxMonth.Items.Add("May");
cboxMonth.Items.Add("June");
cboxMonth.Items.Add("July");
cboxMonth.Items.Add("August");
cboxMonth.Items.Add("September");
cboxMonth.Items.Add("October");
cboxMonth.Items.Add("November");
cboxMonth.Items.Add("December");
cboxMonth.SelectedIndex = 0;
}
void fillYear(int startYear, int endYear)
{
cboxYear.Items.Add("");
for (int i = startYear; i <= endYear; i++)
cboxYear.Items.Add(i);
cboxYear.SelectedIndex = 0;
}
Parse the values of the comboboxes into a DateTime object. 将组合框的值解析为DateTime对象。
DateTime selectedDate = new DateTime(int.Parse(cboxYear.Text), GetMonth(cboxMonth.Text), int.Parse(cboxDay.Text));
Then you can compare the value for your validation 然后,您可以比较该值以进行验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.