#10 Vertical cloud progress bar

open
None
5
2008-10-23
2008-10-20
Erio
No

Hi,

I often need a vertical progress bar, ie. the more percent of the work is done the higher the bar should become.

Can you make a vertical progress bar? I think a property called "orientation" which can be "horizontal" or "vertical" would be good.

Bye,

Erio

Discussion

  • SubsonicDesign
    SubsonicDesign
    2008-10-21

    I can easilly create it. I will also create an orientation property for Cloud TrackBar.

     
  • SubsonicDesign
    SubsonicDesign
    2008-10-23

    • assigned_to: nobody --> dudedesign
     
  • mn_progr
    mn_progr
    2009-04-17

    We added this new feature by modifying the CloudProgressBar.cs file. We added a new property called "Orientation" that has 2 options (Horizontal and Vertical) which will determine the orientation of the progress bar. Here is the code containing this change.

    *** CloudProgressBar.cs Code ***

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

    namespace CloudToolkitN6
    {
    [ToolboxBitmap(typeof(resfinder), "CloudToolkitN6.cloudprogressbar.png")]
    public partial class CloudProgressBar : UserControl
    {
    int min = 0;
    int max = 100;
    int val = 0;
    Orientation_Enum orientation = Orientation_Enum.Horizontal;

    Color ProgressBarColor1 = Color.FromArgb(229, 238, 249);
    Color ProgressBarColor2 = Color.FromArgb(206, 223, 245);
    Color BackColor1 = Color.FromArgb(246, 249, 253);
    Color LineColor = Color.FromArgb(181, 207, 241);
    //Color BackColor2 = Color.CornflowerBlue;

    public CloudProgressBar()
    {
    InitializeComponent();
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    SetStyle(ControlStyles.ResizeRedraw, true);
    }

    protected override void OnResize(EventArgs e)
    {
    this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g = e.Graphics;

    CheckOrientation();

    Rectangle rect = this.ClientRectangle;
    Rectangle overlay = this.ClientRectangle;
    LinearGradientMode gradientMode;

    if (orientation == Orientation_Enum.Horizontal)
    {
    gradientMode =LinearGradientMode.Horizontal;
    }
    else
    {
    gradientMode = LinearGradientMode.Vertical;
    }
    Brush brush = new LinearGradientBrush(rect, ProgressBarColor1, ProgressBarColor2, gradientMode);
    //Brush brush = new LinearGradientBrush(rect, ProgressBarColor2, ProgressBarColor1, gradientMode);
    //Brush bgbrush = new LinearGradientBrush(rect, BackColor1, BackColor2, LinearGradientMode.Vertical);
    Brush bgbrush = new SolidBrush(BackColor1);
    Brush overlaybrush = new SolidBrush(Color.FromArgb(128, Color.White));
    Pen p = new Pen(LineColor, 1);

    float percent = (float)(val - min) / (float)(max - min);
    if (orientation == Orientation_Enum.Horizontal)
    {
    int barwidth = (int)((float)rect.Width * percent);
    rect.Width = barwidth;
    overlay.Height = this.Height / 2;
    }
    else if (orientation== Orientation_Enum.Vertical) {
    int barheight = (int)((float)rect.Height * percent);
    rect.Height = barheight;
    overlay.Width = this.Width / 2;
    }

    g.FillRectangle(bgbrush, this.ClientRectangle);
    g.FillRectangle(brush, rect);
    g.FillRectangle(overlaybrush, overlay);

    drawBorder(g);

    overlaybrush.Dispose();
    bgbrush.Dispose();
    brush.Dispose();
    p.Dispose();
    g.Dispose();
    }

    private void drawBorder(Graphics g)
    {
    Rectangle border = this.ClientRectangle;

    border.Height -= 1;
    border.Width -= 1;

    Pen p = new Pen(LineColor);

    g.DrawRectangle(p, border);

    p.Dispose();
    }

    #region public vars

    public enum Orientation_Enum
    {
    Horizontal,
    Vertical
    }

    public Orientation_Enum Orientation
    {
    get
    {
    return orientation;
    }
    set
    {
    orientation = value;
    }
    }
    public int Minimum
    {
    get
    {
    return min;
    }
    set
    {
    if (value < 0)
    {
    min = 0;
    }

    if (value > max)
    {
    min = value;
    }

    if (val < min)
    {
    val = min;
    }
    this.Invalidate();
    }
    }

    public int Maximum
    {
    get
    {
    return max;
    }
    set
    {
    if (value < min)
    {
    min = value;
    }
    max = value;
    if (val > max)
    {
    val = max;
    }
    this.Invalidate();
    }
    }

    public int Value
    {
    get
    {
    return val;
    }

    set
    {
    int oldValue = val;
    if (value < min)
    {
    val = min;
    }
    else if (value > max)
    {
    val = max;
    }
    else
    {
    val = value;
    }
    float percent;

    Rectangle newValueRect = this.ClientRectangle;
    Rectangle oldValueRect = this.ClientRectangle;
    Rectangle updateRect = new Rectangle();

    if (orientation == Orientation_Enum.Horizontal)
    {
    percent = (float)(val - min) / (float)(max - min);
    newValueRect.Width = (int)((float)newValueRect.Width * percent);
    percent = (float)(oldValue - min) / (float)(max - min);
    oldValueRect.Width = (int)((float)oldValueRect.Width * percent);

    if (newValueRect.Width > oldValueRect.Width)
    {
    updateRect.X = oldValueRect.Size.Width;
    updateRect.Width = newValueRect.Width - oldValueRect.Width;
    }
    else
    {
    updateRect.X = newValueRect.Size.Width;
    updateRect.Width = oldValueRect.Width - newValueRect.Width;
    }
    updateRect.Height = this.Height;
    }
    else if (orientation == Orientation_Enum.Vertical)
    {
    percent = (float)(val - min) / (float)(max - min);
    newValueRect.Height = (int)((float)newValueRect.Height * percent);
    percent = (float)(oldValue - min) / (float)(max - min);
    oldValueRect.Height = (int)((float)oldValueRect.Height * percent);

    if (newValueRect.Height > oldValueRect.Height)
    {
    updateRect.Y = oldValueRect.Size.Height;
    updateRect.Height = newValueRect.Height - oldValueRect.Height;
    }
    else
    {
    updateRect.Y = newValueRect.Size.Height;
    updateRect.Height = oldValueRect.Height - newValueRect.Height;
    }
    updateRect.Width = this.Width;
    }

    this.Invalidate(updateRect);
    }
    }

    public Color BarColor1
    {
    get
    {
    return ProgressBarColor1;
    }
    set
    {
    ProgressBarColor1 = value;
    this.Invalidate();
    }
    }

    public Color BarColor2
    {
    get
    {
    return ProgressBarColor2;
    }
    set
    {
    ProgressBarColor2 = value;
    this.Invalidate();
    }
    }

    public Color BackgroundColor
    {
    get
    {
    return BackColor1;
    }
    set
    {
    BackColor1 = value;
    this.Invalidate();
    }
    }

    public Color BorderColor
    {
    get
    {
    return LineColor;
    }
    set
    {
    LineColor = value;
    this.Invalidate();
    }
    }

    /*public Color BarBackColor2
    {
    get
    {
    return BackColor2;
    }

    set
    {
    BackColor2 = value;

    Invalidate the control to get a repaint.
    this.Invalidate();
    }
    }*/
    #endregion

    #region private methods

    private void CheckOrientation()
    {

    if (orientation == Orientation_Enum.Horizontal)
    {
    if (this.Width < this.Height)
    {
    //this.Top = this.Top - this.Height;
    int temp = this.Width;
    this.Width = this.Height;
    this.Height = temp;
    //this.Location.Y = this.Location.Y - this.Height;
    }
    }
    else if (orientation == Orientation_Enum.Vertical)
    {
    if (this.Width > this.Height)
    {
    //this.Left = this.Left - this.Width;
    int temp = this.Width;
    this.Width = this.Height;
    this.Height = temp;
    //this.Location.X = this.Location.X - this.Width;
    }
    }
    }

    #endregion
    }
    }