ProgressBar is slow in Windows Forms

前端 未结 5 2157
不知归路
不知归路 2020-12-03 07:40

I\'m using Windows Vista and Visual Studio 2010. Create a .Net 4 Windows Forms Application. Drop a progress bar on the default form, add code to handle the form load event a

5条回答
  •  一整个雨季
    2020-12-03 08:19

    I liked Derek W's answer and I managed to find a solution which supports data binding. I inherited from System.Windows.Forms.ProgressBar and created new bindable property. Otherwise it's the same:

    [DefaultBindingProperty("ValueNoAnimation")]
    public class NoAnimationProgressBar : ProgressBar
    {
        /// 
        /// Sets the progress bar value, without using 'Windows Aero' animation.
        /// This is to work around (hack) for a known WinForms issue where the progress bar 
        /// is slow to update. 
        /// 
        public int ValueNoAnimation
        {
            get => Value;
            set
            {
                // To get around the progressive animation, we need to move the 
                // progress bar backwards.
                if (value != Maximum)
                    Value = value + 1; // Move past
                else
                {
                    // Special case as value can't be set greater than Maximum.
                    Maximum = value + 1;
                    Value = value + 1;
                    Maximum = value;
                }
    
                Value = value; // Move to correct value
            }
        }
    }
    

    You can bind to the property like this (viewModel has an int property called Value):

    var dataSource = new BindingSource { DataSource = _viewModel };
    progressBarBindingHack.DataBindings.Add("ValueNoAnimation", dataSource, "Value");
    

提交回复
热议问题