为什么我的Monthcalendar接受了今天的约会?

时间:2014-12-27 18:09:16

标签: c# .net events calendar

这个问题可能听起来很模糊,但我不知道如何以更好的方式表达它。 我一直在制作月历,检查2个选定的日期是否正确。有1个日历用于跟踪代码以确定谁调用了事件(startdate文本框或enddate文本框)

 MaskedTextBox b = (MaskedTextBox)sender;
        currentSelectedDateBox = b.Name;    

开始日期必须早于结束日期,startdate和enddate都不能是已预订的日期(通过将日期设为粗体来显示预订),并且所选期间无法预约在其中。
 这一切都运行正常,虽然我有一个问题:当我打开日历并点击今天,它关闭,意味着用户选择了一个日期,虽然没有显示日期,当我尝试打印所选日期时:它赢了& #39;打印任何东西。但是,如果我选择不同的日期,它将打印该日期,这意味着除了今天之外可以选择所有日期。

如果我点击其他日期然后选择今天,它将起作用。这真的很奇怪,我被卡住了。

这是我的代码:

private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
    {

        ErrorStartDateLabel.Visible = false;
        ErrorEndDateLabel.Visible = false;
        startdateLabel.ForeColor = Color.Black;
        enddateLabel.ForeColor = Color.Black;

        string day = monthCalendar.SelectionStart.Day.ToString();
        string month = monthCalendar.SelectionStart.Month.ToString();
        if (day.Length == 1) //part of the stringbuilder
        {
            day = "0" + day;
        }

        if (month.Length == 1) //part of the stringbuilder
        {
            month = "0" + month;
        }

        string date = day + "-" + month + "-" + monthCalendar.SelectionStart.Year.ToString(); //Date selected
        if (startdateTextbox.Name == currentSelectedDateBox) //If the StartDate Calendar has been selected
        {
            startdateTextbox.Text = date;
            startdate = monthCalendar.SelectionStart.Date;
        }
        else if (enddateTextbox.Name == currentSelectedDateBox)//If the EndDate Calendar has been selected
        {
            enddateTextbox.Text = date;
            enddate = monthCalendar.SelectionStart.Date;
            secondDateChecked = true;
        }
        if (secondDateChecked) //if the enddate textbox has data
        {
            if (enddate < startdate) //if enddate is smaller than the startdate
            {
                enddateTextbox.Text = "";
                ErrorEndDateLabel.Text = "Uw gekozen huurperiode klopt niet!";
                ErrorEndDateLabel.ForeColor = Color.Red;
                enddateLabel.ForeColor = Color.Red;
                ErrorEndDateLabel.Visible = true;
            }
        }

        if (carHasReservation == true) //If there is a reservation
        {
            foreach (var bolddate in bolddates)      //loop through all bolded dates
            {
                if (startdate == bolddate || enddate == bolddate)//if the startdate OR enddate is a bolded date
                {
                    if (startdate == bolddate)
                    {
                        startdateTextbox.Text = "";
                        ErrorStartDateLabel.Text = "Deze startdatum is al gereserveerd!"; 
                        ErrorStartDateLabel.ForeColor = Color.Red;
                        ErrorStartDateLabel.Visible = true;
                    }
                    else
                    {
                        enddateTextbox.Text = "";
                        ErrorEndDateLabel.Text = "Deze einddatum is al gereserveerd!";
                        ErrorEndDateLabel.ForeColor = Color.Red;
                        ErrorEndDateLabel.Visible = true;
                    }
                }
                if (startdate <= enddate)
                {
                    TimeSpan tisp = enddate - startdate;
                    int dateDiffer = tisp.Days;
                    for (int i = 0; i <= dateDiffer; i++) //Count the amount of days between the startdate and the enddate. For every day, check if one of those days is a bolded(reservation) date
                    {
                        if (startdate.AddDays(i) == bolddate)
                        {
                            reservationCollision = true;
                        }
                    }
                }
            }
        }
        else
        {
            //No reservation, do nothing!
        }

        if (reservationCollision) //if there is a boldeddate between the selected dates
        {
            ErrorStartDateLabel.Text = "Tijdens uw geselecteerde periode";
            ErrorEndDateLabel.Text = " is er al een reservering geplaatst!";
            ErrorStartDateLabel.ForeColor = Color.Red;
            ErrorEndDateLabel.ForeColor = Color.Red;
            startdateLabel.ForeColor = Color.Red;
            enddateLabel.ForeColor = Color.Red;
            ErrorStartDateLabel.Visible = true;
            ErrorEndDateLabel.Visible = true;
            enddateTextbox.Text = "";
            reservationCollision = false;
        }

    }    

我的解释和代码可能有点模糊,我很抱歉。我认为最好添加不必要的信息,而不是对我的问题和代码做出错误的解释。

0 个答案:

没有答案