2016-06-12 101 views
-1

感謝您檢查我的線程。標籤文本將不會更新C# - 刷新和Application.DoEvents不起作用

所以,我的小表弟已經在他的學校年底,他需要選擇不同的詞的同義詞這個「考試」。我決定這將是一個很好的機會來練習我的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) 
    { 

    } 
} 

但兩者似乎並沒有改變標籤的價值,當我使用它們在我的主要。

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

     } 

    } 
} 

如果我使用我的構造函數內部的方法,它們就可以工作。刷新也不起作用。

非常感謝你提前,我希望有人能夠幫助我!

+4

如果你剪切並粘貼你的代碼,對每個人來說都容易得多。我建議你使用可變數據的文本框 - 你可以使其成爲只讀的,以便用戶不能更改它。 –

+0

Application.Run(NewForm)是一種模態調用。它不會返回,直到您關閉該表單。沒有辦法看到關閉表單後所做的更改 – Steve

+1

無論如何,執行一個寫入20次相同文本的循環有什麼意義? – Steve

回答

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][]; 

是當你退出程序,那時你的表單已經不存在了。所以你可以把你的邏輯放在構造函數中。或者尋找一種更有效的方式來做到這一點。