0

I am trying to create a calendar using c# that contains a tab for each month of the year with buttons representing the days located on the tabs (see attached picture). The user can input the desired year into a text box and click a button to submit their request (not on attached picture). Currently, I have the calendar working but I cannot figure out how to redraw the calendar when different years are submitted.

I have tried to follow this example https://stackoverflow.com/a/33104430 but I don’t understand when Form_Load() should be called. I have also tried this.refresh() at various places with no avail.

Any help would be much appreciated.

public Form1()
{
    InitializeComponent();
    call_on_load();
}

private void call_on_load()
{   
    pages = tabControl.TabPages;
    year = Convert.ToInt16(textBoxYear.Text);
    dt = new DateTime(year, 1, 1);

    day = -1;
    foreach (TabPage page in pages) //declare a page object and cycle through each tab page 
    {
        if (!initialMonth)
        {
            mth++;         //inc month if not first time.  Originally set.
        }
        initialMonth = false;
        if (mth > 12)  //make a 1 year calendar
            break;
        //ftime = true;
        Console.WriteLine("********************************The date is:" + dt.ToString());
        x = ((((int)dt.DayOfWeek) * 75) + 10);  //reset x coordinate
        y = 20;
        for (int rows = 1; rows <= 7; rows++)  // # of rows in a month
        {                   //Some months have 6 rows.  Use 7 to ensure the below break statement
            if (!ftime)
            {
                if (dt.Day == 1)  //at the top of another month
                {
                    ftime = true;
                    break;
                }
            }

            ftime = false;
            y += 75;       //move y coordinate
            for (int col = 1; col <= 7; col++)  //make 7 columns
            {
                Button b = new Button();
                b.Name = dt.ToString("MMMM") + "_" + Convert.ToString(dt.Day) + "_" + dt.ToString("yyyy"); //store the date in the button name to parse
                b.Click += (s, e) =>      //https://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button
                {
                    secondForm = new Form2();
                    String[] date = b.Name.Split('_');
                    secondForm.setDate(date[0], Convert.ToInt16(date[1]), Convert.ToInt16(date[2]));
                    secondForm.Show();
                };

                b.Size = new Size(50, 50);
                b.Left = x;
                b.Top = y;
                page.Controls.Add(b);  //add button to current tab page 
                                       // btnInt++;                      
                b.Text = Convert.ToString(dt.Day);
                getDate();
                Console.WriteLine("The date is:" + dt.ToString());
                dt = dt.AddDays(1);
                if (dt.Day == 1)
                    break;
                x += 75;
                if (x > 460)  //if x coordinate is at the end of the line
                {
                    x = 10;
                    break;
                }
            }
        }
    }
}

private void btnSubmitF1_Click(object sender, EventArgs e)
{
    year = Convert.ToInt16(textBoxYear.Text);
    //this.Refresh();  //does not redraw
    call_on_load();   //keeps original layout, does not redraw on button click
    //Form_Load(btnSubmitF1,e);  //probably not calling this method correctly.  Is this method needed?
    //this.Refresh();  //does not redraw
}

private void Form_Load(object sender, EventArgs e)
{
    call_on_load();  
}
Community
  • 1
  • 1
IBWEV
  • 11
  • 1
  • 6
  • 1
    The code in the part `//setup buttons representing days on each tab page` is actually what we'd need to see to to work out what's going wrong. Generally, though, you don't need to do anything special to make the page redraw if you are adding the control properly. – Enigmativity Oct 10 '16 at 04:06
  • I must be adding the control improperly. I add a button and then add the button to the tab on the form. – IBWEV Oct 10 '16 at 04:26
  • Can you please show the code where you set `mth` outside of the `call_on_load` method? – Enigmativity Oct 10 '16 at 05:02

2 Answers2

0

I don't think the issue is to do with refreshing the page. I think you're simply not resetting the mth variable and then the if (mth > 12) is always being hit. However, you haven't shown enough code for us to tell for sure.

Also, your code doesn't seem to be very well structured. There's lots of stuff going on that could cause you grief.

To help out I re-wrote the code for you in a way that I think would help.

Try this:

private void call_on_load()
{
    var year = Convert.ToInt16(textBoxYear.Text);
    var dt = new DateTime(year, 1, 1);

    var months =
        Enumerable
            .Range(0, dt.AddYears(1).Subtract(dt).Days)
            .Select(d => dt.AddDays(d))
            .GroupBy(x => x.Month);

    foreach (var month in months)
    {
        var tab = tabControl.TabPages[month.Key - 1];
        tab.Controls.Clear();
        var firstDayOfWeek = (int)month.First().DayOfWeek;
        foreach (var date in month)
        {
            var position = firstDayOfWeek + date.Day - 1;
            var button = new Button()
            {
                Size = new Size(50, 50),
                Left = (position % 7) * 75 + 10,
                Top = (position / 7) * 75 + 20,
                Text = date.ToShortDateString(),
            };
            button.Click += (s, e) =>
            {
                var secondForm = new Form2();
                secondForm.setDate(date);
                secondForm.Show();
            };
            tab.Controls.Add(button);
        }
    }
}

I tested this and it seemed to work just fine.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Wow. Thanks for your time and knowledge. The code is working and you are correct in that I never reset mth. – IBWEV Oct 10 '16 at 13:18
-1
 //you just missing a postback maybe, try this.
 private void Form_Load(object sender, EventArgs e)
  {
    if(!IsPostBack){
    call_on_load(); 

   }
  }

edited

   private void btnSubmitF1_Click(object sender, EventArgs e)
     {
    year = Convert.ToInt16(textBoxYear.Text);
    //this.Refresh();  //does not redraw
    call_on_load();   //keeps original layout, 
      //does not redraw on button click
    //Form_Load(btnSubmitF1,e);  //probably not calling 
    //this method correctly.  Is this method needed?
    //this.Refresh();  //does not redraw
    this.ParentForm.Refresh();
    }
this.hart
  • 218
  • 2
  • 9