How can I make an endless progressbar in WinForms?

后端 未结 6 1967
独厮守ぢ
独厮守ぢ 2020-12-05 21:11

I don\'t know how long an action could take and I want to display a progress bar to the user in a dialog box. I\'ve tried using System.Windows.Forms.ProgressBar but it doesn

相关标签:
6条回答
  • 2020-12-05 21:20

    There might be a better way, but one way is just to set Value back to 0 when it reaches the end (assuming your task isn't complete)

    0 讨论(0)
  • 2020-12-05 21:23

    I found Chris Lawl's solution the best, very good and clean solution just include a gif http://www.ajaxload.info/ and no mess creating never ending progress bar.

    0 讨论(0)
  • 2020-12-05 21:28

    System.Windows.Forms.ProgressBar has a property called Style. Setting Style to Marquee will achieve the effect your looking for.

    EDIT: Divo points out the Marquee Style is only available on

    Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003

    The comments provide even more information indicating that this appears to work everywhere as long as you're using .NET 2.0 or higher.

    0 讨论(0)
  • 2020-12-05 21:36

    Have you tried setting the Style property of the System.Windows.Forms.ProgressBar to Marquee?

    However, surprisingly, this property is only available on the following platforms (according to MSDN):

    Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003

    Might be that the documentation has not been updated to Vista though. Anyone knows about a limitation on Vista?

    EDIT: As posted in another comment the documentation seems to be wrong with respect to the supported platforms. Should be working on Vista as well as Windows 7.

    0 讨论(0)
  • 2020-12-05 21:36

    Just use an animated gif :)

    You can make your own here: http://www.ajaxload.info/

    0 讨论(0)
  • 2020-12-05 21:36

    This is what worked for me. I create a indeterminate progressbar for you. Add an custom control to your project/form and insert this code:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace AnimatedCustomControls
    {
      sealed class IndeterminateProgressbar : Control
      {
        private readonly List<int> positions = new List<int>();
        private readonly Timer tmrAnimation = new Timer {Interval = 5, Enabled = false};
        private readonly Timer tmrAddPosition = new Timer {Interval = 500, Enabled = true};
    
    
        public Color ProgressColor { get; set; }
        public Color InactiveColor { get; set; }
    
        public IndeterminateProgressbar()
        {
            DoubleBuffered = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            ProgressColor = Color.FromArgb(40, 190, 245);
            InactiveColor = Color.FromArgb(40, 40, 40);
            tmrAnimation.Tick += tmrAnimation_Tick;
            tmrAddPosition.Tick += tmrAddPosition_Tick;
            if (!DesignMode) tmrAnimation.Start();
        }
    
        void tmrAddPosition_Tick(object sender, EventArgs e)
        {
            positions.Add(1);
        }
    
        void tmrAnimation_Tick(object sender, EventArgs e)
        {
            if (DesignMode) tmrAnimation.Stop();
            for (int i = 0; i < positions.Count; i++)
            {
                positions[i] += 2 + Math.Abs(positions[i]) / 50;
                if (positions[i] > Width) positions.RemoveAt(i);
            }
            Invalidate();
        }
    
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            if (Enabled)
            {
                positions.Clear();
                positions.AddRange(new[] { Width / 10, Width / 3, Width / 2, (int)(Width * 0.7) });
            }
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            if (Enabled)
            {
                e.Graphics.Clear(BackColor);
                foreach (int i in positions)
                {
                    e.Graphics.DrawLine(new Pen(Brushes.Black, 4f), i, 0, i, Height);
                }
            }
            else e.Graphics.Clear(InactiveColor);
    
            base.OnPaint(e);
        }
    }
    

    }

    You should then build your solution and when you go back to the designer, the new control should be in your toolbox. Drag it into you form, set the maximum and minimum value and that's all.

    I've created a sample program to let you know how it is used:

            private void Form1_Load(object sender, EventArgs e)
        {
            indeterminateProgressbar1.BackColor = Color.FromArgb(40, 190, 245); //it's an nice color ;)
            indeterminateProgressbar1.Size = new Size(400, 4); //make it small in the height looks better
            indeterminateProgressbar1.Visible = true;
        }
    
    0 讨论(0)
提交回复
热议问题