【问题标题】:How can I delete all Panels from my calendar?如何从我的日历中删除所有面板?
【发布时间】:2017-01-26 08:16:36
【问题描述】:

我现在正在用 C# 编写日历。 如果我调用我的日历,它会创建与当前月份一样多的面板。但是,如果我想将当前月份增加一,则当前月份的面板仍然存在。 因此,我必须在更改月份后立即删除所有面板。 但是在这种情况下我该怎么做呢?

感谢您的帮助。

代码说明: 首先我调用 createPanel 方法,为当月创建面板。 接下来,如果我单击 MonthAdd 方法,我想删除我创建的所有面板。

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();
    }


}

}

【问题讨论】:

  • 忽略 createPanel 方法中的 bool xD 我忘了删除它...

标签: c# calendar


【解决方案1】:
       ...
        for (int i = 0; i <= numDays; i++)
        {
                //Create Panel
                test[i] = new Panel(); 
        }

        ...

然后

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

【讨论】:

    【解决方案2】:

    我不确定我是否很好地理解了你,但我想到的第一个简单的解决方案是,在创建面板时,你可以将它们保存在一些列表中,例如,在生成新月份之前,你可以调用

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

    【讨论】:

    • 谢谢,听起来合乎逻辑。但是,现在我遇到的问题是我不知道如何将具有所有属性的对象存储在列表中。抱歉,刚开始用 C# 编程:/
    • 我假设您一天中的所有内容都将包含在您创建的每个面板中。如果在您的循环中,您将添加每个面板 panel.Add(test);例如 List panel = new List();那么您将能够访问您的每个面板及其所有内容
    • 请记住,您在该列表中有对面板的引用,它们与您创建的对象相同。
    • 哦,不错 :) 现在我明白了。谢谢
    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2015-04-04
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    相关资源
    最近更新 更多