0

I'm attempting to create a calendar app in C# using a windows forms application. I've stumbled open a problem when attempting to add days to my groupbox.text labels more than once when clicking a button. Apparently it works the first time perfectly, but when I hit the button the second time nothing happens. I have a clue it might be because it's not saving the new value I assign the date, but I am unsure

private void loadDays()
        {
            // Get the current time and display the day
            gbBoks.Text = currentDate.ToString("dd");

            // Get the current time and display the day tomorrow by adding a day
            gbBoks2.Text = currentDate.AddDays(1).ToString("dd");

            gbBoks3.Text = currentDate.AddDays(2).ToString("dd");

            gbBoks4.Text = currentDate.AddDays(3).ToString("dd");

            gbBoks5.Text = currentDate.AddDays(4).ToString("dd");

            gbBoks6.Text = currentDate.AddDays(5).ToString("dd");

            gbBoks7.Text = currentDate.AddDays(6).ToString("dd");

            gbBoks8.Text = currentDate.AddDays(7).ToString("dd");

            gbBoks9.Text = currentDate.AddDays(8).ToString("dd");

            gbBoks10.Text = currentDate.AddDays(9).ToString("dd");
        }

        private void loadNextWeek()
        {
            TimeSpan aWeek = new System.TimeSpan(7, 0, 0, 0);

            // Get the current time and display the day
            gbBoks.Text = currentDate.AddDays(10).ToString("dd");

            // Get the current time and display the day tomorrow by adding a day
            gbBoks2.Text = currentDate.AddDays(11).ToString("dd");

            gbBoks3.Text = currentDate.AddDays(12).ToString("dd");

            gbBoks4.Text = currentDate.AddDays(13).ToString("dd");

            gbBoks5.Text = currentDate.AddDays(14).ToString("dd");

            gbBoks6.Text = currentDate.AddDays(15).ToString("dd");

            gbBoks7.Text = currentDate.AddDays(16).ToString("dd");

            gbBoks8.Text = currentDate.AddDays(17).ToString("dd");

            gbBoks9.Text = currentDate.AddDays(18).ToString("dd");

            gbBoks10.Text = currentDate.AddDays(19).ToString("dd");
        }

        private void loadPreviousWeek()
        {
            gbBoks.Text = currentDate.AddDays(-10).ToString("dd");

            // Get the current time and display the day tomorrow by adding a day
            gbBoks2.Text = currentDate.AddDays(-9).ToString("dd");

            gbBoks3.Text = currentDate.AddDays(-8).ToString("dd");

            gbBoks4.Text = currentDate.AddDays(-7).ToString("dd");

            gbBoks5.Text = currentDate.AddDays(-6).ToString("dd");

            gbBoks6.Text = currentDate.AddDays(-5).ToString("dd");

            gbBoks7.Text = currentDate.AddDays(-4).ToString("dd");

            gbBoks8.Text = currentDate.AddDays(-3).ToString("dd");

            gbBoks9.Text = currentDate.AddDays(-2).ToString("dd");

            gbBoks10.Text = currentDate.AddDays(-1).ToString("dd");
        }

EDIT: Button eventhandling

        private void BtnNext_Click(object sender, EventArgs e)
        {
            loadNextWeek();
            lblInfo.Text = "Went one week forward!";
        }

        private void BtnPrevious_Click(object sender, EventArgs e)
        {
            loadPreviousWeek();
            lblInfo.Text = "Travelled one week back!";
        }

        private void BtnRefresh_Click(object sender, EventArgs e)
        {
            loadDays();
            lblInfo.Text = "Returned to starting point!";
        }

No errors occured, only that I expected the days to be added again once the button was clicked.

(Keep in mind the code listed are the methods I have called in the button eventhandlers)

  • You haven't posted any code related to the event handling of the button press. Can you edit your question and provide some more information, please? – zhulien Jun 09 '19 at 15:34
  • You're correct. [`AddDays`](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.adddays?view=netframework-4.8) will return a new `DateTime` with additions made to the current value. – ChiefTwoPencils Jun 09 '19 at 15:40
  • As an aside, you could also consider keeping refs to those labels in an array and do this work in a loop. – ChiefTwoPencils Jun 09 '19 at 15:44
  • Possible duplicate of [DateTime.AddDays() not working as expected](https://stackoverflow.com/questions/11583601/datetime-adddays-not-working-as-expected) – ChiefTwoPencils Jun 09 '19 at 15:45
  • I seemed to have resolved the issue. You were right @ChiefTwoPencils - It was a duplicate problem from [DateTime.AddDays() not working as expected](https://stackoverflow.com/questions/11583601/datetime-adddays-not-working-as-expected) I fixed it by adding this at the end of the methods (loadNextWeek() and loadPreviousweek()): currentDate = currentDate.AddDays(7); – FredTheNoob Jun 09 '19 at 16:11

0 Answers0