Reputation: 319
I've been searching and trying things for little less than an hour. I don't think it should be this hard to do but for what ever reason No matter what I change I get the same error when using the monthcalander to select a date.
I'm not sure if I'm doing it some where or if it is C# being temperamental with me.
Some general description of the form controls in use - There are two text boxes (textBox3 and textBox4) two button (button5 and button6) and two monthcalendars( monthcalendar1 and 2)
When you click on either button it will show the month calendar for that button, button5->monthcalendar1, and button6->monthcalendar2: you can select a date, and it will put that date into the corresponding textBox, mc1->textBox3, mc2->textBox4
Now that you should have some details about what controls go where... This is the code I am using when a user selects a date, this is similar for both mc1 and 2, so i will likely only post the code for 1 of them....
private void monthCalendar2_DateSelected(object sender, DateRangeEventArgs e)
{
textBox4.Text = monthCalendar2.SelectionStart.ToShortDateString();
monthCalendar2.Location = new Point(306, 204);
monthCalendar2.Visible = false; //306,204
}
it stores the data in the textBox like so: m/d/yyyy,
So I have been trying to get the difference between the date stored in textBox3, and textBox4; and have tried numourous configurations of code to try to get it to work, all come back with a format exception code error when teh date for textBox4 is selected::
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
var startDate = DateTime.Parse(textBox3.Text);//, "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture);
var endDate = DateTime.Parse(textBox4.Text);//, "M/d/yyyy"), System.Globalization.CultureInfo.InvariantCulture);
TimeSpan dateDiff = endDate - startDate;
string message = "";
if (endDate < startDate)
{
message = "Your departure date must be before your return date.";
}
else if(dateDiff.TotalDays == 0 || dateDiff.TotalDays == 1)
{
message = "Only planning one day of travel, please select one of the other options or select a longer period of travel.";
}
else if(dateDiff.TotalDays >= 7)
{
message = "Traveling less than or equal to seven days is not enough, select another option or a longer travel period.";
}
else
{
message = "Have a wonderful trip!";
}
MessageBox.Show(message, "Trip Notice");
}
I have tried to use:
DateTime startDate, endDate;
startDate = DateTime.ParseExact(textBox3.Text,"M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture);
endDate = DateTime.ParseExact(textBox4.Text,"M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture);
That too didn't work, so I tried::
DateTime startDate, endDate;
startDate = Convert.toDateTime(textBox3.Text); //did the same for textBox4
It didn't work either. I'm at a complete loss, and most of these posts here are having me run in circles. Please help.
Upvotes: 2
Views: 782
Reputation: 37060
It's most likely because DateChanged
is called before DateSelected
, and your Text
property is empty. This should work:
private void monthCalendar2_DateChanged(object sender, DateRangeEventArgs e)
{
// Update the text in TextBox4 first...
textBox4.Text = monthCalendar2.SelectionStart.ToShortDateString();
var startDate = DateTime.Parse(textBox3.Text);
var endDate = DateTime.Parse(textBox4.Text);
// Rest of the code omitted...
I think there's other problems with the logic of your code, but see if this works.
P.S. This is the other logic problem...you are using "greater than or equal to" instead of "less than or equal to"...
else if (dateDiff.TotalDays >= 7)
{
message = "Traveling less than or equal to seven days is not enough, ...";
}
Upvotes: 2