locked
Enable a Button when Progress bar has Loaded Completely. RRS feed

  • Question

  • Hello, Please I'm working on a Splash Screen for my application.

    I want a situation where when the Progress Bar has loaded completely then my button to start the application would be Enabled.

    Microsoft Visual C#.

    Thanks

    Saturday, April 16, 2016 11:32 AM

Answers

  • When I read Magnus' introduction I thought I understood what he had in mind.

    The code doesn't seem to match that though.

    You haven't shown any code, but you have a progressbar and you've presumably got some code that sets the percentage for that.

    Here's a bit of code which doesn't do anything other than set the value on a progressbar.

    ( You are presumably actually doing something as the progress is increased ).

            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                timer1.Start();
                timer1.Interval = 1000;
                timer1.Tick += new EventHandler(timer1_Tick);
            }
    
            void timer1_Tick(object sender, EventArgs e)
            {
                if (CustomProgressBar1.Value < 100)
                {
                    CustomProgressBar1.Value += 5;
                    this.label1.Text = string.Format("{0:p0}", (double)CustomProgressBar1.Value / 100);
                    CustomProgressBar1.Invalidate();
                }
                else
                {
                    timer1.Stop();
                }
            }


    You could set the button enabled in that last else.

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                timer1.Start();
                timer1.Interval = 1000;
                timer1.Tick += new EventHandler(timer1_Tick);
                btnStart.Enabled = false;
            }
    
            void timer1_Tick(object sender, EventArgs e)
            {
                if (CustomProgressBar1.Value < 100)
                {
                    CustomProgressBar1.Value += 5;
                    this.label1.Text = string.Format("{0:p0}", (double)CustomProgressBar1.Value / 100);
                    CustomProgressBar1.Invalidate();
                }
                else
                {
                    timer1.Stop();
                    btnStart.Enabled = true;
                }
            }

    Have you considered setting Visible to false initially and true on completion?

    That would IMO be clearer than disabling.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Proposed as answer by Spacemullah Saturday, April 16, 2016 2:30 PM
    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:34 AM
    Saturday, April 16, 2016 1:23 PM
  • The ProgressBar has loaded completely when you set its Value property for the last time...it cannot know when the loading is completed unless you tell it. A ProgressBar doesn't automatically determine how long time it takes for an operation to complete.

    If you want an if-case, you could check if the Value of the ProgressVar is equal to its Maximum:

                if(pb.Value == pb.Maximum)
                    button1.Enabled = false;

    But again, you as a developer are responsible for setting the Value property at regular intervals so you might as well set the Enabled property of the Button when you set the Value property to its final value as I suggested in my previous post.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:34 AM
    Saturday, April 16, 2016 1:24 PM

All replies

  • Since you need to set the Value property of the ProgressBar at regular intervals, you could set the Enabled property of the Button to true when you set the Value property of the ProgessBar for the last time, i.e. when the loading is complete:
    ...
                pb.Value = 100;
                button1.Enabled = true;

    Don't forget to set the Enabled property of the Button to false initially:

    public SplashForm()
            {
                InitializeComponent();
         button1.Enabled = false;
     }


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, April 16, 2016 11:38 AM
  • Thanks,

    But i wan't it to Enable the Button after the Progress Bar must have loaded completely.

    Maybe using an if(condition)

    Thanks.

    Saturday, April 16, 2016 1:09 PM
  • When I read Magnus' introduction I thought I understood what he had in mind.

    The code doesn't seem to match that though.

    You haven't shown any code, but you have a progressbar and you've presumably got some code that sets the percentage for that.

    Here's a bit of code which doesn't do anything other than set the value on a progressbar.

    ( You are presumably actually doing something as the progress is increased ).

            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                timer1.Start();
                timer1.Interval = 1000;
                timer1.Tick += new EventHandler(timer1_Tick);
            }
    
            void timer1_Tick(object sender, EventArgs e)
            {
                if (CustomProgressBar1.Value < 100)
                {
                    CustomProgressBar1.Value += 5;
                    this.label1.Text = string.Format("{0:p0}", (double)CustomProgressBar1.Value / 100);
                    CustomProgressBar1.Invalidate();
                }
                else
                {
                    timer1.Stop();
                }
            }


    You could set the button enabled in that last else.

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                timer1.Start();
                timer1.Interval = 1000;
                timer1.Tick += new EventHandler(timer1_Tick);
                btnStart.Enabled = false;
            }
    
            void timer1_Tick(object sender, EventArgs e)
            {
                if (CustomProgressBar1.Value < 100)
                {
                    CustomProgressBar1.Value += 5;
                    this.label1.Text = string.Format("{0:p0}", (double)CustomProgressBar1.Value / 100);
                    CustomProgressBar1.Invalidate();
                }
                else
                {
                    timer1.Stop();
                    btnStart.Enabled = true;
                }
            }

    Have you considered setting Visible to false initially and true on completion?

    That would IMO be clearer than disabling.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Proposed as answer by Spacemullah Saturday, April 16, 2016 2:30 PM
    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:34 AM
    Saturday, April 16, 2016 1:23 PM
  • The ProgressBar has loaded completely when you set its Value property for the last time...it cannot know when the loading is completed unless you tell it. A ProgressBar doesn't automatically determine how long time it takes for an operation to complete.

    If you want an if-case, you could check if the Value of the ProgressVar is equal to its Maximum:

                if(pb.Value == pb.Maximum)
                    button1.Enabled = false;

    But again, you as a developer are responsible for setting the Value property at regular intervals so you might as well set the Enabled property of the Button when you set the Value property to its final value as I suggested in my previous post.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Kristin Xie Monday, April 25, 2016 7:34 AM
    Saturday, April 16, 2016 1:24 PM