开发者

Show the new form after Progress Bar percentage completed c# project

开发者 https://www.devze.com 2023-02-15 23:19 出处:网络
I am working on a project using Visual Studio(c#). I want to create a startup form when i install my application with a progress bar. And after progress bar completed开发者_StackOverflow中文版this for

I am working on a project using Visual Studio(c#). I want to create a startup form when i install my application with a progress bar. And after progress bar completed开发者_StackOverflow中文版 this form should be hide and a new form should be open. can u help me about this problem?


Edit:

I've just made a sample application trying to use exactly the code that you've specified. It worked fine besides just one tweak:

Form1().Show(); should be new Form1().Show();

The only way this code does not execute is if you forgot to set timer1 to enabled state in design view which causes the code to never fire up.

Are you sure the code is firing up? have you done a break-point on this piece of code?

On a sidenote: timer1 is not on a separate thread so you don't need to use Invoke (you can see if you actually need it by looking InvokeRequired property of a control)

Suggested improvement: if you are not going to use Form2 again and judging from your code, it is likely you won't; perhaps you should call Close() on Form2 instead of Hide() and release the resources. I've had times when my application kept running in background because I hid the form but never closed it and application was on "exit when last window closes" which never happened.

So to be sure, here is the final code that does work on my machine:

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();
        }
    }
}


  1. Create your form and add your progress bar
  2. Set up event handlers on the parts of the form that should effect the progress bar
  3. Update the progree bar to reflect the amount of work that is done
  4. When the form is complete close it


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;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消