user6538028
user6538028

Reputation:

How can I delete all Panels from my calendar?

I'm programming an calendar with C# right now. If i call my calendar, it creates as much panels as the current month has days. But if I want to increase the current month by one, the panels from the current month are stilt there. So i have to delete all my panels as soon as I change the month. But how can i do it in this case ?

Thanks for the help.

Code eplain: First I call the createPanel method, to create panels for the current month. Next if I click the MonthAdd method, I want to delete all my created panels.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;



namespace Einteilungs_Plan
{

public partial class Kalender : Form
{
    public DateTime TodayDate { get; set; }

    int counting = 0;

    public Kalender()
    {
        InitializeComponent();

        //Kalenderwochen initialisieren
        monat(counting);
        createPanel(true);


    }




    public string monat(int adding)
    {
        string monat = DateTime.Now.AddMonths(adding).ToString("MMMM");
        tbMonat.Text = monat;
        return monat;
    }

    private void btnAddMonth_Click(object sender, EventArgs e)
    {


        counting++;



        if(counting < 12)
        {
            monat(counting);
            switch (counting)
            {
                case 0:
                    int number = 10;
                    break;

                case 1:

                    break;

                default:
                    break;

            }
        }
        else
        {
            counting--;
        }
    }


    private void btnRemoveMonth_Click(object sender, EventArgs e)
    {


        counting--;
        if (counting > -1)
        {
            monat(counting);
        }
        else
        {
            counting++;
        }
    }

    public void createPanel(bool remove)
    {

        var numDays = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);

            int locationX = 12;
            int locationY = 74;

            for (int i = 0; i <= numDays; i++)
            {
                //Create Panel
                Panel test = new Panel();
                //Fill Panel
                test.Name = "panel" + i;
                test.Width = 200;
                test.Height = 100;
                test.BackColor = Color.White;
                test.Location = new System.Drawing.Point(locationX, locationY);
                this.Controls.Add(test);
                test.Show();
                if(i == 6 || i == 13 || i == 20 || i == 28)
                {
                    locationY += 106;
                    locationX = -194;
                }
                locationX += 206;

            }
    }



    public void Kalender_Shown(object sender, EventArgs e)
    {


    }

    private void Kalender_Load(object sender, EventArgs e)
    {

    }

    private void btnNeuerEintrag_Click(object sender, EventArgs e)
    {

        Formular formular = new Formular();
        formular.Show();
        formular.Focus();

    }

    private void btnHinzufügen_Click(object sender, EventArgs e)
    {
        Formular formular = new Formular();
        formular.Show();
        formular.Focus();
    }


}

}

Upvotes: 2

Views: 42

Answers (2)

Paweł Swajdo
Paweł Swajdo

Reputation: 391

I'm not sure if I undestood you well but first simple solutiona that comes into my mind is that while creating panels you can keep them in some List for example and before generating new month you can call

 foreach (var p in panels)
            this.Controls.Remove(p);

Upvotes: 0

Adam Klon
Adam Klon

Reputation: 46

       ...
        for (int i = 0; i <= numDays; i++)
        {
                //Create Panel
                test[i] = new Panel(); 
        }

        ...

and then

            this.Control.Remove(test[i]);

Upvotes: 1

Related Questions