I want to have one windows form with one progress bar on it to appear with the progress bar getting filled upto maximum to give some visual loading application type of effect. I have written some code to do this task, and it worked fine in one of my previous application but in my current application when I am using the same code which I did in the previous application the progress bar just gets filled upto 10% and then stops. Can anyone provide the solution. The code is as below:

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

namespace Cyber_Application
{
    public partial class SerialCheck : Form
    {
        public SerialCheck()
        {
            InitializeComponent();
        }

        Login login = new Login();

        private void SerialCheck_Load(object sender, EventArgs e)
        {
            timer1.Start();

            timer1.Interval += 1;
            progressBar1.Value += 10;
            if (progressBar1.Value == 100)
                timer1.Stop();
            if (progressBar1.Value == 100)
            {
                progressBar1.Hide();
                this.Hide();
            }
            if (progressBar1.Value == 100)
                login.Show();
        }
    }
}

Try This:

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

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

        //Login login = new Login();

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
    
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(1);
            if (progressBar1.Value == 100)
                timer1.Stop();
            if (progressBar1.Value == 100)
            {
                progressBar1.Hide();
                this.Hide();
            }
            if (progressBar1.Value == 100)
                //login.Show();
                //I Add This As Notes in Order to Debug Without Errors
                MessageBox.Show("It Works!", "Your Welcome ;)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}

This works for me so I hope it does for you. Also, if you need to control the speed, just change the interval of the Timer control via the Properties menu.

Hmm, and where is a Timer reference? Times instantiation code?
And why are 3 if blocks (same one) needed? You only create one.
Take a look.

Full code should look like:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//for this code bellow you dont need  Threading namespace!!!

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //Timer is from Windows.Froms namespace!
        Timer timer1;
        Login login;
        public Form1()
        {
            InitializeComponent();
            //timer instantiation:
            timer1 = new Timer();
            timer1.Interval = 1000; //every second will be called an event Tick
            timer1.Tick += new EventHandler(timer1_Tick);             
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //start timer:
            timer1.Start();    
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(1);
            if (progressBar1.Value == 100)
            {
                timer1.Stop();
                progressBar1.Hide();
                this.Hide();
                login = new Login();
                login.Show();
                //I Add This As Notes in Order to Debug Without Errors
            }
        }
    }

    //login form:
     public partial class Form1 : Form
     {
          public Login()
          {
               InitializeComponent();
               MessageBox.Show("It Works, and welcome on login form!", "Welcome note.)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          }
     }
}

The reason you DO NOT need to create the Timer using code is because you can drag the Timer component in from the ToolBox and just double click it to create the timer1_Tick Event. Also, I was just using his code for the timer1_Tick Event because it was late and I was very tired to do it ;)

Your code works exactly as mine does. Look below, there is essentially no difference.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        //Login login = new Login();
 
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(1);

            if (progressBar1.Value == 100)
               {
                timer1.Stop();
                progressBar1.Hide();
                this.Hide();
                //login.Show();
                //I Add This As Notes in Order to Debug Without Errors
        }
    }
}

Edited 5 Years Ago by NetJunkie: n/a

This question has already been answered. Start a new discussion instead.