我有兩個表格mainForm和helperForm。在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();
}
如果要從Click事件處理程序執行大量代碼,那麼您需要使用線程來避免該代碼造成用戶界面無響應。插入helperForm.Update()調用是一個非常不完善但簡單的解決方法。 –
@HansPassant我沒有刪除helperForm.Close(),但在所有進程完成後仍然出現文本。 – user2611251
你的代碼片段很不清楚,它隱藏了真正的問題。我們無法看到的代碼阻止了控件自己繪製。如前所述,在每次AppendText()調用後調用helperForm.Update()都會強制控件繪製,以便您可以看到它。我強烈推薦一本關於Winforms編程的書,瞭解這一點並不重要,而且非常重要,可以在編寫適當的GUI代碼方面取得進展。 –