Hi,
I need help on my Windows Application in C#.
I have startTimeTextBox which accepts only time format like 09:00 AM and durationTextBox that accepts time format like 05:00 (without AM/PM on it) then I need them to be added and put the sum to the endTimeTextBox.
e.g. 09:00 AM + 05:00 = 03:00 PM
Below is my code for the startTimeTextBox and DurationTextBox
private void StartTimeTextBox_Validated(object sender, EventArgs e)
{
int text = startTimeTextBox.Text.Length;
if (text >= 5)
{
Regex checktime = new Regex(@"(([0-1][0-9])|([2][0-3])):([0-5][0-9])");
string sample = startTimeTextBox.Text.Substring(0, 5);
if (!checktime.IsMatch(sample))
{
MessageBox.Show("Invalid input in Start Time!");
startTimeTextBox.Clear();
}
else if(startTimeTextBox.Text.Substring(5,1) != " ")
{
MessageBox.Show("Invalid input in Start Time!");
startTimeTextBox.Clear();
}
else
{
string TimeCheck = startTimeTextBox.Text.Substring(6, 2);
if (TimeCheck != "AM" && TimeCheck != "PM")
{
MessageBox.Show("Invalid input in Start Time!");
startTimeTextBox.Clear();
}
}
}
else
{
MessageBox.Show("Invalid input in Start Time!");
startTimeTextBox.Clear();
}
}
private void DurationTextBox_Validated(object sender, EventArgs e)
{
int text = durationTextBox.Text.Length;
if (text >= 5)
{
Regex checktime = new Regex(@"(([0-1][0-9])|([2][0-3])):([0-5][0-9])");
string sample = durationTextBox.Text.Substring(0, 5);
if (!checktime.IsMatch(sample))
{
MessageBox.Show("Invalid input in Start Time!");
durationTextBox.Clear();
}
}
else
{
MessageBox.Show("Invalid input in Start Time!");
durationTextBox.Clear();
}
}
Thanks,
MukuroRokudo