最小年龄验证GUI c#
本文关键字:GUI 验证 | 更新日期: 2023-09-27 18:11:10
我要验证一个顾客至少25岁,以便租车。我相信我有正确的代码来计算客户类中的年龄,但它实际上并没有在应用
的代码中验证它。代码来自customer.cs
public int Age
{
get
{
// Get today's date
DateTime currDate = DateTime.Now;
// Get the difference in years
int age = currDate.Year - birthdayPicker.Year;
// Subtract another year if we're before the
// birth day in the current year
if (currDate.Month < birthdayPicker.Month || (currDate.Month == birthdayPicker.Month && currDate.Day < birthdayPicker.Day))
age--;
return age;
}
}
来自form .cs的代码(仍然在应用按钮上工作,但在我继续之前试图验证年龄)
private void applyButton_Click(object sender, EventArgs e)
{
if (myCust.Age >= (25))
{
maketextbox.Text = myVehicle.Make;
modelTextbox.Text = myVehicle.Model;
yearTextbox.Text = Convert.ToString(myVehicle.Year);
vinTextbox.Text = Convert.ToString(myVehicle.Vin);
MessageBox.Show("Congratulations, you have been approved to rent a car!");
}
else
{
MessageBox.Show("You are not old enough to rent a car.");
}
可能是比较代码出错:
// Subtract another year if we're before the
// birth day in the current year
if (currDate.Month < birthdayPicker.Month || (currDate.Month >= birthdayPicker.Month && currDate.Day < birthdayPicker.Day))
age--;
注意: = = 操作符更改为> =