在另一个表单中打开一个表单?

时间:2013-09-22 13:05:07

标签: c# winforms

我有两个名为 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();
}

2 个答案:

答案 0 :(得分: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();

     }

你正在无模式地显示一个表单,这意味着这个不会阻止调用者线程,因此没有理由在Form.Close()之后调用Form.Show() 6个语句,这没有意义,这只是显示flash的表单,然后表单消失。如果您需要在指定的时间内显示表单,请更好地使用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秒,然后它会自动关闭。

答案 1 :(得分: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(); }));
    }
}
相关问题