标签文本不会更新C# - 刷新和Application.DoEvents不起作用

时间:2016-06-12 21:25:08

标签: c# text label refresh

感谢您查看我的帖子。

所以,我的小表弟在他的学年末有这个'考试',在那里他需要选择各种单词的同义词。我认为这是练习我的C#技能(非常不存在)的好机会。 最初我开始将程序编写为一个简单的Python脚本。

但是,我认为如果有一个实际的程序接口,孩子应该更容易学习这些东西,所以我决定使用Visual C#来创建这个“简单”的测验 - 这是练习语言的好机会。

我希望程序更改标签,根据问题 - 我在Form1中编写了两个函数

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public string LabelText
    {
        get
        {
            return this.label12.Text;
        }
        set
        {
            this.label12.Text = value;
        }
    }

    public void UpdateQuestion(string QNum)
    {
      LabelText = QNum;
      label12.Refresh();
      Application.DoEvents();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void label11_Click(object sender, EventArgs e)
    {

    }

    private void label12_Click(object sender, EventArgs e)
    {

    }
}

但是当我在Main中使用它们时,两者似乎都没有改变标签的值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 NewForm = new Form1();
        Application.Run(NewForm);


        string[][] list = new string[10][];

        list[0] = new[] { "abandon", "leave" };
        list[1] = new[] { "abbreviate", "shorten" };
        list[2] = new[] { "option", "choice" };
        list[3] = new[] { "Inferior", "lesser", "second-class", "second-fiddle", "minor", "subservient", "lowly", "humble", "menial" };
        list[4] = new[] { "Nauseous", "sick", "nauseated", "queasy", "bilious" };
        list[5] = new[] { "Uniform", "constant", "consistent", "steady", "invariable", "unvarying", "unfluctuating", "unvaried", "unchanging", "unwavering", "undeviating", "stable", "static", "sustained", "regular", "fixed", "even", "equal", "equable", "monotonous" };
        list[6] = new[] { "Incision", "cut", "opening", "slit" };
        list[7] = new[] { "perplexed", "puzzled" };
        list[8] = new[] { "polarity", "difference", "separation", "opposition", "contradiction" };
        list[9] = new[] { "Abundance", "profusion", "plenty", "wealth", "copiousness" };

        for (int counter = 0; counter < 20; counter++) {
            NewForm.UpdateQuestion("Question Number");

        }

    }
}

如果我使用我的构造函数中的方法,他们的工作。刷新也不起作用。

提前非常感谢,我希望有人能够帮助我!

1 个答案:

答案 0 :(得分:0)

  using System;
  using System.Collections.Generic;
 using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
 {
    /// <summary>
       /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 NewForm = new Form1();
        Application.Run(NewForm); // program stops executing here and goes to draw your form



         //program resumes execution here after you close your form.
        string[][] list = new string[10][];

        list[0] = new[] { "abandon", "leave" };
        list[1] = new[] { "abbreviate", "shorten" };
        list[2] = new[] { "option", "choice" };
        list[3] = new[] { "Inferior", "lesser", "second-class", "second-fiddle", "minor", "subservient", "lowly",         "humble", "menial" };
        list[4] = new[] { "Nauseous", "sick", "nauseated", "queasy", "bilious" };
        list[5] = new[] { "Uniform", "constant", "consistent", "steady", "invariable", "unvarying", "unfluctuating",     "unvaried", "unchanging", "unwavering", "undeviating", "stable", "static", "sustained", "regular", "fixed", "even", "equal", "equable", "monotonous" };
        list[6] = new[] { "Incision", "cut", "opening", "slit" };
        list[7] = new[] { "perplexed", "puzzled" };
        list[8] = new[] { "polarity", "difference", "separation", "opposition", "contradiction" };
        list[9] = new[] { "Abundance", "profusion", "plenty", "wealth", "copiousness" };

        for (int counter = 0; counter < 20; counter++) {
        NewForm.UpdateQuestion("Question Number");

        }

    }
}

问题在于代码的结构。 在应用程序启动一个新表单后,它会在Application.Run(NewForm)行之后停止执行代码; 执行该行后,应用程序将呈现新表单并等待您与表单进行交互。程序执行的唯一时间到达行

string[][] list = new string[10][];

是退出程序的时候,那时你的表单已经消失了。所以你可以把你的逻辑放在构造函数中。或者寻找一种更有效的方法。