• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 783
  • Last Modified:

Progress Bar Back Color - Change?

Is there a way to change the progress bar back color? Not certain if iti matters but am on a mobile form?
0
tbaseflug
Asked:
tbaseflug
  • 2
  • 2
  • 2
1 Solution
 
vinhnlCommented:
Hi Tbaseflug, see this sample: http://www.codeproject.com/cs/miscctrl/StatusColorProgressBar.asp

Good luck,
VINHNL
0
 
tbaseflugAuthor Commented:
Not certain if these will work in a mobile app
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bob LearnedCommented:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

[DefaultProperty("Value")]
public class ProgressBarEx : System.Windows.Forms.Control
{
  private const Int32 WM_USER = 1024;
  private const Int32 PBM_SETRANGE = (WM_USER + 1);
  private const Int32 PBM_SETPOS = (WM_USER + 2);
  private const Int32 PBM_DELTAPOS = (WM_USER + 3);
  private const Int32 PBM_SETSTEP = (WM_USER + 4);
  private const Int32 PBM_STEPIT = (WM_USER + 5);
  private const Int32 PBM_SETRANGE32 = (WM_USER + 6);
  private const Int32 PBM_GETRANGE = (WM_USER + 7);
  private const Int32 PBM_GETPOS = (WM_USER + 8);
  private const Int32 CCM_FIRST = 8192;
  private const Int32 PBM_SETBKCOLOR = (CCM_FIRST + 1);
  private const Int32 PBM_SETBARCOLOR = (WM_USER + 9);

  [DllImport("comctl32.dll")]
  private static extern bool InitCommonControlsEx(INITCOMMONCONTROLS icc);

  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);

  public ProgressBarEx()
  {
    this.Minimum = 0;
    this.Maximum = 100;
    this.Step = 10;
    this.Value = 0;
    base.SetStyle((ControlStyles.Selectable | ControlStyles.UserPaint), false);
  }

  protected override void CreateHandle()
  {
    if (!(base.RecreatingHandle))
    {
      INITCOMMONCONTROLS initcommoncontrolsex1 = new INITCOMMONCONTROLS();
      initcommoncontrolsex1.dwICC = 32;
      InitCommonControlsEx(initcommoncontrolsex1);
    }
    base.CreateHandle();
  }

  public void Increment(int value)
  {
    this.Value = (this.Value + value);
    if ((this.Value < this.Minimum))
    {
      this.Value = this.Minimum;
    }
    if ((this.Value > this.Maximum))
    {
      this.Value = this.Maximum;
    }
    this.UpdatePos();
  }
  private Color m_barColor = SystemColors.ActiveCaption;

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public Color BarColor
  {
    get
    {
      return m_barColor;
    }
    set
    {
      m_barColor = value;
      SendMessage(new HandleRef(this, this.Handle), PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(this.BarColor));
    }
  }

  protected override void OnHandleCreated(EventArgs e)
  {
    base.OnHandleCreated(e);
    SendMessage(new HandleRef(this, this.Handle), PBM_SETRANGE32, this.Minimum, this.Maximum);
    SendMessage(new HandleRef(this, this.Handle), PBM_SETSTEP, this.Step, 0);
    SendMessage(new HandleRef(this, this.Handle), PBM_SETPOS, this.Value, 0);
    SendMessage(new HandleRef(this, this.Handle), PBM_SETBARCOLOR, 0, ColorTranslator.ToWin32(this.BarColor));
    SendMessage(new HandleRef(this, this.Handle), PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(this.BackColor));
  }

  public void PerformStep()
  {
    this.Increment(this.Step);
  }

  public override string ToString()
  {
    string text1 = base.ToString();
    return string.Concat(new string[]{text1, ", Minimum: ", this.Minimum.ToString(), ", Maximum: ", this.Maximum.ToString(), ", Value: ", this.Value.ToString()});
  }

  private void UpdatePos()
  {
    if (base.IsHandleCreated)
    {
      SendMessage(new HandleRef(this, this.Handle), PBM_SETPOS, this.Value, 0);
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public override bool AllowDrop
  {
    get
    {
      return base.AllowDrop;
    }
    set
    {
      base.AllowDrop = value;
    }
  }
  private Color m_backColor = SystemColors.Control;

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public override Color BackColor
  {
    get
    {
      return m_backColor;
    }
    set
    {
      m_backColor = value;
      SendMessage(new HandleRef(this, this.Handle), PBM_SETBKCOLOR, 0, ColorTranslator.ToWin32(this.BackColor));
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public override Image BackgroundImage
  {
    get
    {
      return base.BackgroundImage;
    }
    set
    {
      base.BackgroundImage = value;
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public new bool CausesValidation
  {
    get
    {
      return base.CausesValidation;
    }
    set
    {
      base.CausesValidation = value;
    }
  }

  protected override CreateParams CreateParams
  {
    get
    {
      CreateParams params1 = base.CreateParams;
      params1.ClassName = "msctls_progress32";
      return params1;
    }
  }

  protected override ImeMode DefaultImeMode
  {
    get
    {
      return ImeMode.Disable;
    }
  }

  protected override Size DefaultSize
  {
    get
    {
      return new Size(100, 23);
    }
  }

  [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  public override Font Font
  {
    get
    {
      return base.Font;
    }
    set
    {
      base.Font = value;
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public override Color ForeColor
  {
    get
    {
      return base.ForeColor;
    }
    set
    {
      base.ForeColor = value;
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public new ImeMode ImeMode
  {
    get
    {
      return base.ImeMode;
    }
    set
    {
      base.ImeMode = value;
    }
  }

  [Description("ProgressBarMaximumDescr"), RefreshProperties(RefreshProperties.Repaint), DefaultValue(100), Category("CatBehavior")]
  public int Maximum
  {
    get
    {
      return _maximum;
    }
    set
    {
      if ((this.Maximum != value))
      {
        if ((value < 0))
        {
          throw new ArgumentException("Invalid maximum argument");
        }
        if ((this.Minimum > value))
        {
          _minimum = value;
        }
        _maximum = value;
        if ((this.Value > this.Maximum))
        {
          this.Value = this.Maximum;
        }
        if (base.IsHandleCreated)
        {
          SendMessage(new HandleRef(this, this.Handle), PBM_SETRANGE32, this.Minimum, this.Maximum);
          this.UpdatePos();
        }
      }
    }
  }

  [RefreshProperties(RefreshProperties.Repaint), Description("ProgressBarMinimumDescr"), Category("CatBehavior"), DefaultValue(0)]
  public int Minimum
  {
    get
    {
      return _minimum;
    }
    set
    {
      if ((this.Minimum != value))
      {
        if ((value < 0))
        {
          throw new ArgumentException("Invalid lower bound argument: Minimum");
        }
        if ((this.Maximum < value))
        {
          _maximum = value;
        }
        _minimum = value;
        if ((this.Value < this.Minimum))
        {
          this.Value = this.Minimum;
        }
        if (base.IsHandleCreated)
        {
          SendMessage(new HandleRef(this, this.Handle), PBM_SETRANGE32, this.Minimum, this.Maximum);
          this.UpdatePos();
        }
      }
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public override RightToLeft RightToLeft
  {
    get
    {
      return base.RightToLeft;
    }
    set
    {
      base.RightToLeft = value;
    }
  }

  [Description("ProgressBarStepDescr"), DefaultValue(10), Category("CatBehavior")]
  public int Step
  {
    get
    {
      return _step;
    }
    set
    {
      _step = value;
      if (base.IsHandleCreated)
      {
        SendMessage(new HandleRef(this, this.Handle), PBM_SETSTEP, this.Step, 0);
      }
    }
  }

  [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public new bool TabStop
  {
    get
    {
      return base.TabStop;
    }
    set
    {
      base.TabStop = value;
    }
  }

  [Bindable(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
  public override string Text
  {
    get
    {
      return base.Text;
    }
    set
    {
      base.Text = value;
    }
  }

  [DefaultValue(0), Description("ProgressBarValueDescr"), Bindable(true), Category("CatBehavior")]
  public int Value
  {
    get
    {
      return _value;
    }
    set
    {
      if ((this.Value != value))
      {
        if (((value < this.Minimum) || (value > this.Maximum)))
        {
          throw new ArgumentException("Invalid bound argument");
        }
        _value = value;
        this.UpdatePos();
      }
    }
  }
  private int _maximum;
  private int _minimum;
  private int _step;
  private int _value;
  [StructLayout(LayoutKind.Sequential, Pack=1)]
    private class INITCOMMONCONTROLS
  {

    public INITCOMMONCONTROLS()
    {
      this.dwSize = 8;
    }
    public int dwICC;
    public int dwSize;
  }
}

Sample:
   this.progressBar1.BarColor = Color.Red;
   this.progressBar1.BackColor = Color.White;

Bob
0
 
tbaseflugAuthor Commented:
Bob -

Will this work in a mobile app
0
 
Bob LearnedCommented:
I overlooked "mobile" :(  

I have no idea, but I don't think so.

Bob
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now