So I am making a windows form with a separate class for student details and international student. I need to validate that my Year mark for the student/international student is More than 0 and less than 100.
I would Also like to get the date of birth to show as dd/mm/yyyy but currently it shows as that plus the time.
I will include code below:
Date of birth first
student1.DateOfBirth = DateTime.Parse(txtDateofbirth.Text).Date;
if (string.IsNullOrEmpty(txtDateofbirth.Text))
{
MessageBox.Show("Please enter a DoB");
}
Now for the Year Mark
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");
}
I would recommend you to use a DateTimePicker control for entering dates and a NumericUpDown control to enter the YearMark, instead of using TextBox.
The DateTimePicker control allows you to define the display format and restrict entry to a certain range. If I remember correctly the Value property is of type DateTime so you don’t have to parse a string.
The NumericUpDown control allows you to define a range of allowed values. The Value property is of type Decimal, but you can safely cast that to an Int32. That’s a lot safer than parsing a String.