2013-09-22 51 views
-4

我有兩個表格mainFormhelperForm。在mainForm上我有一個按鈕,在helperForm上我有一個richTextBox。我想要做的是;一旦點擊mainForm上的按鈕,我想顯示helperForm,以及richtextbox上的一些文本。下面的代碼,我能看到的輔助形式,但文字按鈕後,即將完成所有的buttonClick事件中去的過程中...在另一個表單中打開一個表單?

的MainForm

public partial class Form : Form 
{ 
    public HelperForm helperForm; 

    public MainForm() 
    { 
    InitializeComponent(); 
    } 

    public void button_Click(object sender, EventArgs e) 
    { 
    helperForm= new HelperForm(); 
    helperForm.Show();  

    helperForm.richTextBox1.AppendText("Program started"); 

    //doing process1 
    helperForm.richTextBox1.AppendText("Program start to check process1"); 

    //doing process2 
    helperForm.richTextBox1.AppendText("Program start to check process2"); 

    //doing process3 
    helperForm.richTextBox1.AppendText("Program start to check process3"); 

    //doing process2 
    helperForm.richTextBox1.AppendText("All the process are done!"); 

    helperForm.Close(); 
} 
+1

如果要從Click事件處理程序執行大量代碼,那麼您需要使用線程來避免該代碼造成用戶界面無響應。插入helperForm.Update()調用是一個非常不完善但簡單的解決方法。 –

+0

@HansPassant我沒有刪除helperForm.Close(),但在所有進程完成後仍然出現文本。 – user2611251

+0

你的代碼片段很不清楚,它隱藏了真正的問題。我們無法看到的代碼阻止了控件自己繪製。如前所述,在每次AppendText()調用後調用helperForm.Update()都會強制控件繪製,以便您可以看到它。我強烈推薦一本關於Winforms編程的書,瞭解這一點並不重要,而且非常重要,可以在編寫適當的GUI代碼方面取得進展。 –

回答

0

試試這個;

public void button_Click(object sender, EventArgs e) 
    { 
     helperForm= new HelperForm(); 
     Timer countdown=new Timer(); 
     countdown.Interval=1000;//1000 Milliseconds 
     countdown.Tick+=CountDownTickEvent; 

     switch(condition to check which text to append): 
     { 
     case 1: 
     helperForm.richTextBox1.AppendText("Program started"); 
     break; 

     case 2: 
     //doing process1 
     helperForm.richTextBox1.AppendText("Program start to check process1"); 
     break; 

     case 3: 
     //doing process2 
     helperForm.richTextBox1.AppendText("Program start to check process2"); 
     break; 

     case 4: 
     //doing process3 
     helperForm.richTextBox1.AppendText("Program start to check process3"); 
     break; 

     case 5: 
     //doing process2 
     helperForm.richTextBox1.AppendText("All the process are done!"); 
     break; 
     } 

     helperForm.Show(); 

    } 

你是無模式顯示形式,它意味着這個人是不會阻塞調用者線程,因此沒有理由6個發言,而不是Form.Show()以後調用Form.Close(),它沒有任何意義,這只是顯示形式閃爍,然後表單消失。如果您需要在指定的時間內顯示錶單,最好使用Timer

編輯:要顯示指定持續時間的表單,請使用下面的函數;

int counter=0; 
    public void button_Click(object sender, EventArgs e) 
    { 
     helperForm= new HelperForm(); 
     Timer countdown=new Timer();//New instance of Timer class. 
     countdown.Interval=1000;//1000 Milliseconds,change as needed. 
     countdown.Tick+=CountDownTickEvent;//Event handler for tick event of Timer. 

     //Same as above. 

     helperForm.Show(); 
     countdown.Start();//Start the timer. 
    } 

然後創建如下的CountDownTickEvent;

private void CountDownTickEvent(object sender, EventArgs e) 
    { 
     counter++; 
     if (counter == 5)//5 seconds have been passed since the timer is running.Change as needed. 
     { 
      helperForm.Close();//Displayed for 5 seconds,form should close now. 
      counter=0; 
     } 
    } 

好了,給你一個helperForm 5秒文本,然後它會自動關閉。

0

正確的解決方法是使用一個後臺線程來實現顯示器的「進度條」類型,這防止鎖定UI。當從後臺線程指揮UI操作時,您需要調用這些操作來防止異常 - UI更新不能直接從後臺線程中進行。

下面的代碼是做你想做的,但你需要閱讀線程,調用和委託,否則這是沒有任何意義的。如果我們可以看到更多的代碼,那麼可能有一種方法可以使用Application.DoEvents(),但是在循環中更合適,它提供了重複命令的好地方。除非我將一個小型應用程序放在一起,否則我仍然更喜歡後臺線程。

public partial class Form1 : Form 
{ 
    private HelperForm helperForm; 
    private Thread processRunner; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (processRunner == null || processRunner.ThreadState == ThreadState.Stopped) 
     { 
      helperForm = new HelperForm(); 
      helperForm.Show(); 

      processRunner = new Thread(new ThreadStart(runProcesses)); 
      processRunner.Start(); 
     } 
    } 

    private void runProcesses() 
    { 


     this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("Program started"); })); 

     //represents process1 
     Thread.Sleep(2000); 

     this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("Program start to check process1"); })); 

     //represents process2 
     Thread.Sleep(2000); 

     this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("Program start to check process2"); })); 

     //represents process3 
     Thread.Sleep(2000); 

     this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("Program start to check process3"); })); 

     //represents process4 
     Thread.Sleep(2000); 

     this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("All the process are done!"); })); 

     // to let the message display 
     Thread.Sleep(2000); 

     helperForm.Invoke(new Action(() => { helperForm.Close(); })); 
    } 
} 
相關問題