2011-03-09 122 views
1

我正在使用Visual Studio(C#)一个项目之后的新形式。我想创建一个启动窗体,当我用进度条安装我的应用程序。进度条完成后,应该隐藏这个表单,并打开一个新表单。你能帮我解决这个问题吗?显示进度条完成百分比C#项目

+0

我们可以看到一些代码吗?提供进度栏值是什么? – 2011-03-09 14:57:27

回答

0

编辑:

我刚刚作出了一个示例应用程序试图准确地使用您所指定的代码。它工作得很好,除了只有一个好办法:

Form1().Show();应该new Form1().Show();

这个代码不执行的,如果你忘了设置timer1enabled状态导致代码永远不会火起来的设计视图中的唯一方法。

你确定代码发射了?你有没有在这段代码上做一个突破点?

在一个旁注: TIMER1是不是一个单独的线程,所以你不需要使用调用(你可以看到,如果你真的需要它通过寻找控制的InvokeRequired财产)

建议改进:,如果你不打算再次使用窗体2,并从你的代码来看,很可能你不会;也许你应该叫Form2上,而不是Hide()Close()并释放资源。我曾经有次在我的应用程序在后台运行,因为我隐藏了表单,但从未关闭它,并且应用程序处于“最后一个窗口关闭时退出”状态,这种情况从未发生过。

所以可以肯定,这里是做我的机器上工作的最终代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

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

      //enable timer1 here or in designer 
      timer1.Enabled = true; 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      //disable timer1 first thing, otherwise it can end up ticking 
      //multiple times before you've had a chance to disable it 
      //if the timespan is really short 
      timer1.Enabled = false; 

      int d; 

      for (d = 0; d <= 100; d++) 
       progressBar1.Value = d; 

      Hide(); 

      //create a new Form1 and then show it 
      new Form1().Show(); 
     } 
    } 
} 
+0

我也使用form2上的调用,但它不会隐藏,以显示窗体1. – vivek 2011-03-12 07:00:51

+0

查看新的代码。希望它能解决你的问题。对不起,我的回应有所延误。 – Maverik 2011-03-16 12:49:24

+0

抱歉,表格2未关闭。 – vivek 2011-03-27 10:53:36

0
  1. 创建窗体并添加您的进度条
  2. 建立在形式应该影响进度条
  3. 更新progree栏,以反映完成
  4. 的工作量的部分事件处理程序
  5. 当表单完成后关闭它
0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

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

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      int d; 

      for (d = 0; d <= 100; d++) 
       progressBar1.Value = d; 

      this.Hide(); 
      Form1().Show(); 


      timer1.Enabled = false; 
     } 
    } 
} 
+2

而不是在您的答案中发布代码,编辑原始帖子以添加新的/更新的内容。如果有很多反应,但大家需要看到您提供的代码回答可能会丢失。 – Maverik 2011-03-10 15:11:34