Hello everyone,
I have tried the code for start and stop time and been successful in doing so.Now I am trying to compare values of start and stop time as shown below in the code. I have used two masked txtbox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace LoginPage
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }




        private void button1_Click(object sender, EventArgs e)
        {
            string start,stop,check;
            CultureInfo info = new CultureInfo("en-US");
            start = maskedTextBox1.Text;
            DateTime start1 = DateTime.ParseExact(start,"HH:mm",info);
            stop = maskedTextBox2.Text;
            DateTime stop1 = DateTime.ParseExact(stop, "HH:mm", info);
            check = maskedTextBox2.Mask;
            DateTime check1 = DateTime.ParseExact(check,"HH:mm",info);

            if (start1 >= stop1)
            {
               MessageBox.Show("Invalid entry","Warning");
            }

           MessageBox.Show("The start time is: " + maskedTextBox1.Text + "\n" + "The stop time is: " + maskedTextBox2.Text);

            MessageBox.Show("System time-" + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString());

        }

         public void Form3_Load(object sender, EventArgs e)
        {
            // Other initialization code
            maskedTextBox1.Mask = "00:00";
            maskedTextBox1.ValidatingType = typeof(System.DateTime);
            maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
            maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);

            maskedTextBox2.Mask = "00:00";
            maskedTextBox2.ValidatingType = typeof(System.DateTime);
            maskedTextBox2.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox2_TypeValidationCompleted);
            maskedTextBox2.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox2_MaskInputRejected);

        }


    }
}

I have used the handlers that I've defined above.Alz working well. But, the problem is wid the If condition used in ButtonClick Event above. The check has value of "00:00" strored in it.Now I want to enter the start time as today' timei.e. after "22:00" and the end time should be of next day's early morning i.e. between "03:00" to "07:00". So, i am having trouble with comparing the values of start1 and stop1 along with check1.As d value of stop1 is after "00:00" it is not giiving proper validation. Plz see if someone can help.

Recommended Answers

All 6 Replies

Firstly, I would consider using the DateTimePicker to get the time values as this provides better validation of times that the MaskedTextBox.

Secondly, if you are allowing the end time to be after midnight then there is no automatic validation that you can do to check if the entered time values are correct.

Best thing you can do is ask the user for confirmation before committing the times.

Firstly, I would consider using the DateTimePicker to get the time values as this provides better validation of times that the MaskedTextBox.

Secondly, if you are allowing the end time to be after midnight then there is no automatic validation that you can do to check if the entered time values are correct.

Best thing you can do is ask the user for confirmation before committing the times.

Hey, what about if I store the min and max value in some variable and compare the stop time with it and check for validation

What is it you are trying to validate? You can't use start > stop or start < stop because you haven't got a date to determine order. For instance, if start date is 22:00 and stop date is 07:00 you cant tell if its 7am the following day (which is valid) or 7am the same day (which isn't).
If you want the times to be within a certain range (eg stop is between 03:00 - 07:00) then you can validate the entry against this requirement: if stop > 03:00 && stop < 07:00. Other than that you just have to assume that the end date is after the first or (as Nick.Crane suggested) display what you think the user intended and have them confirm it before you commit it.

hi Ryshad, I have the problem with validating the stop time.As the stop time is to be entered after 00:00 AM so the problem occurs. So to know that the morning time is of next day, I have thought of using the min and max variable which stores the times of "00:00" and "12:00"of next day,so if stop time occurs betwwen these values,then it can be incremented by one day using stop.AddDays(1). I think this will work but whether it will be good coding practice to compare with values stored in variable. Otherwise the suggestion given by Nick.Crane should be implemented regarding the confirmation.
Thanks to both of you.

Thats what i suggested; sorry if i didnt word it clearly enough.
You can ensure the time falls between a given range (00:00 and 12:00) and if it is within that range then 'assume' it is the next day (ie stop.AddDays(1)). This is a good approach if you want to process the times without user involvement. This ultimately depends on your design; if the start date must be the following morning then this approach is fine. However, if your design allows the user to have an alarm set for 2 days after the start date, or a time in the evening of the same day, then this approach wont work.

Hi Ryshad, I tried the code as I had said by using two variables min and max, and its working.Ofcourse, my design wants the time for only one day so it will work for next day's morning and even for the same day's evening,only under the condition that stop time should be greater than start time.Its working for both these cases. So, I think the problem is solved and I should mark the thread as solved.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.