【问题标题】:Reloading Form in C#在 C# 中重新加载表单
【发布时间】:2017-02-18 10:46:49
【问题描述】:

我正在尝试使用 c# 创建一个日历,其中包含一年中每个月的选项卡,其中的按钮代表选项卡上的日期(见附图)。用户可以在文本框中输入所需的年份,然后单击按钮提交他们的请求(不在附图上)。目前,我可以使用日历,但我不知道在提交不同年份时如何重绘日历。

我尝试按照这个示例进行操作 https://stackoverflow.com/a/33104430,但我不明白什么时候应该调用 Form_Load()。我也在各个地方尝试过this.refresh(),但无济于事。

任何帮助将不胜感激。

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

【问题讨论】:

  • //setup buttons representing days on each tab page 部分中的代码实际上是我们需要查看以找出问题所在。不过,通常情况下,如果您正确添加控件,则无需执行任何特殊操作来重绘页面。
  • 我必须不正确地添加控件。我添加了一个按钮,然后将该按钮添加到表单上的选项卡中。
  • 您能否显示您在call_on_load 方法之外设置mth 的代码?

标签: c# forms reload


【解决方案1】:

我认为问题与刷新页面无关。我认为您根本没有重置mth 变量,然后if (mth &gt; 12) 总是被击中。但是,您没有显示足够的代码让我们确定。

另外,您的代码结构似乎不是很好。有很多事情会导致你悲伤。

为了帮助您,我以我认为会有所帮助的方式为您重新编写了代码。

试试这个:

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

我对此进行了测试,它似乎工作得很好。

【讨论】:

  • 哇。感谢您的时间和知识。代码正在运行,您是正确的,因为我从未重置 mth。
【解决方案2】:
 //you just missing a postback maybe, try this.
 private void Form_Load(object sender, EventArgs e)
  {
    if(!IsPostBack){
    call_on_load(); 

   }
  }

编辑

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

【讨论】:

  • 这个问题似乎是关于 WinForms,而不是 ASP.NET。
  • 你从哪里得到this.ParentForm
猜你喜欢
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多