【发布时间】:2012-10-18 21:18:07
【问题描述】:
所以我正在制作一个带有单独班级的窗口表格,用于学生详细信息和国际学生。我需要验证我的学生/国际学生的年级分数是否大于 0 且小于 100。
我还想让出生日期显示为 dd/mm/yyyy,但目前它显示为那个加上时间。
我将在下面包含代码:
先出生日期
student1.DateOfBirth = DateTime.Parse(txtDateofbirth.Text).Date;
if (string.IsNullOrEmpty(txtDateofbirth.Text))
{
MessageBox.Show("Please enter a DoB");
}
现在是年关
try // Trying to do the thing in the curly braces
{
student1.YearMark = Int32.Parse(txtYearMark.Text);
}
catch (Exception) // If it doesn't work skip the crash and print this
{
MessageBox.Show("The Year mark must be a number and can't be empty");
}
【问题讨论】:
-
这不是一个完整的答案,但请尝试在 DateTime 上使用 TryParse 方法,然后查看生成的 DateTime 实例的各种属性。
标签: c# validation parsing datetime