How would you create a validate statement that shows a message to the user if they enter a value that is 5 years or older than the current date, I have the checking of format for the date if its entered correctly.

Currently I have this to valid the format that the user inputs the two dates:

private void CalBtn_Click(object sender, EventArgs e)
        {



            // Definate the DateTime var for the formula below.

            try
            {
                DateTime arrival;
                DateTime departure;
                // show how many days between the Departure day and the current day. (On Calculate Btn Click)
                arrival = Convert.ToDateTime(arrivalTxb.Text);  //Convert arrival date to text.
                departure = Convert.ToDateTime(departTxb.Text);  //Convert departure date to text.
                TimeSpan nighttotal = departure - arrival; // departure minus arrival = number of days
                int nights = nighttotal.Days;
                nnightTxb.Text = nights.ToString(); // show on Number of Nights text box.

                // Calculate price of total days requests.  Price is a static $115
                int subtotal = nights * 115;  // Total = nights (number of days staying above) times 115 (price per night).
                totalTxb.Text = subtotal.ToString("c"); // show total in Total Price textbox formatted.

            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid format.  Please try again!", "Entry Error");
            }
        }

Thanks for any help or hints.

simply use the 'Value Changed' event of the DateTimePicker control.

if (dateTimePicker1.Value > DateTime.Now.AddYears(5))
        MessageBox.Show("Date should be within the next 5 years");

simply use the 'Value Changed' event of the DateTimePicker control.

if (dateTimePicker1.Value > DateTime.Now.AddYears(5))
        MessageBox.Show("Date should be within the next 5 years");

I think it was meant to say < -5 given you want to see if the date is 5 years older than current:

if (dateTimePicker1.Value < DateTime.Now.AddYears(-5))

Well..yeah.. wasent sure if he ment 5 years ahead of today or 5 years before today! I assumed its checking the date to be withing 5 years ahead of today.

Well..yeah.. wasent sure if he ment 5 years ahead of today or 5 years before today! I assumed its checking the date to be withing 5 years ahead of today.

Second set of eyes never hurts--LOL. I went to back to see if I had the comparison operator correct--I was going to blame it on Michelob Brewing Co if not. ;)

Cheers!

I think it was meant to say < -5 given you want to see if the date is 5 years older than current:

if (dateTimePicker1.Value < DateTime.Now.AddYears(-5))

Yeah if the user input is lets say 2015 that would make it invalid.
I'm still slightly confused on the if (dateTimePicker1.Value) line, what would I use to valid the dateTimePicker is I don't have the DateTimePicker and its just user Inputted format. I.E. I don't have that awesome little calendar that would come up (if I'm thinking correctly). I can add that, but its really need needed.

Thanks

In that case you will have to parse the input using the following:

DateTime input = DateTime.Parse(textBox.text);
// you can now use the variable input to check..

You will have to make sure that the input is in correct format. Put this in a try catch to avoid application to crash.

I'm confused a little. I understand if you cannot use the date picker control... Can you explain with a briefer section of code that shows exactly what you are trying to but cannot achieve?

I'm confused a little. I understand if you cannot use the date picker control... Can you explain with a briefer section of code that shows exactly what you are trying to but cannot achieve?

private void CalBtn_Click(object sender, EventArgs e)
        {



            // Definate the DateTime var for the formula below.

            try
            {
                DateTime arrival;
                DateTime departure;
                // show how many days between the Departure day and the current day. (On Calculate Btn Click)
                arrival = Convert.ToDateTime(arrivalTxb.Text);  //Convert arrival date to text.
                departure = Convert.ToDateTime(departTxb.Text);  //Convert departure date to text.
                TimeSpan nighttotal = departure - arrival; // departure minus arrival = number of days
                int nights = nighttotal.Days;
                nnightTxb.Text = nights.ToString(); // show on Number of Nights text box.

                // Calculate price of total days requests.  Price is a static $115
                int subtotal = nights * 115;  // Total = nights (number of days staying above) times 115 (price per night).
                totalTxb.Text = subtotal.ToString("c"); // show total in Total Price textbox formatted.

            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid format.  Please try again!", "Entry Error");
            }
        }

Thats the whole code here the part that needs to be validated so that the year doesn't explain beyond 5 years:

arrival = Convert.ToDateTime(arrivalTxb.Text);  //Convert arrival date to text.
                departure = Convert.ToDateTime(departTxb.Text);

The Arrival and departure. I need to make it so that if the user enters 5yr and 1 day past today's date it show a msg that say: "You can't not enter that, blah blah blah"

Hope that helps a bit maybe? Again I might be taking this in the wrong direction lol.

Put the code for Converting to datetime and validating in the 'Leave' event handler for the textbox.

Put the code for Converting to datetime and validating in the 'Leave' event handler for the textbox.

Sorry you really lost me here. I think I know what your talking about, so I apologize in advance if I sound stupid, but I'm not sure what you mean. Do you mean move the converting to datetime and time to the outter part of {}?

I think I'll check back in the morning I'll have a cleaning head for this :S. Sorry if I sound dumb at the moment.

I trust you will know what to do with this:

DateTime arrival = new DateTime();
            DateTime departure = new DateTime();
            TimeSpan ts = arrival - departure;
            if (ts.Days > 5 * 365)
                MessageBox.Show("Houston, we have a problem....");

I trust you will know what to do with this:

DateTime arrival = new DateTime();
            DateTime departure = new DateTime();
            TimeSpan ts = arrival - departure;
            if (ts.Days > 5 * 365)
                MessageBox.Show("Houston, we have a problem....");

Yeah, thank you for the calcification on this. I definitely hit my limit on programming\drinking tonight :S. I'll work with this in the morning more thank you for the help.

Is there a reason why you don't use DateTimePickers on your form?
They are as easy to use as a textbox and will save you the trouble of validating a date input format in a textbox.

Is there a reason why you don't use DateTimePickers on your form?
They are as easy to use as a textbox and will save you the trouble of validating a date input format in a textbox.

We need to use user input, per directions. Although we can use a date time picker, I rather use user input text because I have other purposes for this after I'm finished the basic core of this.

But don't get me wrong, I'll all for the easy way to do things with DateTimePickers, but I have some ideas to use this in other places later on that I rather not have to go back and edit my stuff.

Thanks again for the help.

I got all excited and then forgot I still got to valid to make sure that the arrival date can't be entered past the current date and that the departure date is after the arrival date ~_~, ugh I hate validations lol.

As I said, if you use DataTimePickers, you can set a MaxDate property!
No more validation! You can not select a date past this MaxDate, so you don't have to do any checking and you won't annoy your users with (in this case) horrible messageboxes.

As I said, if you use DataTimePickers, you can set a MaxDate property!
No more validation! You can not select a date past this MaxDate, so you don't have to do any checking and you won't annoy your users with (in this case) horrible messageboxes.

Yeah I agree and just went with a date time picker and changed it a bit to suit my needs.

Again sometimes MessageBox are more useless than having the user click 1000times only to know that it won't work ;).

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.