1

I am creating my own custom progress bar control for windows CE .Net 2.0 C# Application. Below is the code which works fine the only thing missing is I want to show progress Right to left can anyone help me in modifying this code to get RTL property

Code Refering

using System;
using System.Drawing;
using System.Windows.Forms;
class CustomProgressBar:UserControl
{
    int min = 0;    // Minimum value for progress range
    int max = 100;  // Maximum value for progress range
    int val = 0;        // Current progress
    Color BarColor = Color.Blue;        // Color of progress meter
    protected override void OnResize(EventArgs e)
    {
        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        SolidBrush brush = new SolidBrush(BarColor);
        float percent = (float)(val - min) / (float)(max - min);
        Rectangle rect = this.ClientRectangle;

        // Calculate area for drawing the progress.
        rect.Width = (int)((float)rect.Width * percent);

        // Draw the progress meter.
        g.FillRectangle(brush, rect);

        // Draw a three-dimensional border around the control.
        Draw3DBorder(g);

        // Clean up.
        brush.Dispose();
        g.Dispose();
    }

    public int Minimum
    {
        get
        {
            return min;
        }

        set
        {
            // Prevent a negative value.
            if (value < 0)
            {
                min = 0;
            }

            // Make sure that the minimum value is never set higher than the maximum value.
            if (value > max)
            {
                min = value;
                min = value;
            }

            // Ensure value is still in range
            if (val < min)
            {
                val = min;
            }

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

    public int Maximum
    {
        get
        {
            return max;
        }

        set
        {
            // Make sure that the maximum value is never set lower than the minimum value.
            if (value < min)
            {
                min = value;
            }

            max = value;

            // Make sure that value is still in range.
            if (val > max)
            {
                val = max;
            }

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

    public int Value
    {
        get
        {
            return val;
        }

        set
        {
            int oldValue = val;

            // Make sure that the value does not stray outside the valid range.
            if (value < min)
            {
                val = min;
            }
            else if (value > max)
            {
                val = max;
            }
            else
            {
                val = value;
            }

            // Invalidate only the changed area.
            float percent;

            Rectangle newValueRect = this.ClientRectangle;
            Rectangle oldValueRect = this.ClientRectangle;

            // Use a new value to calculate the rectangle for progress.
            percent = (float)(val - min) / (float)(max - min);
            newValueRect.Width = (int)((float)newValueRect.Width * percent);

            // Use an old value to calculate the rectangle for progress.
            percent = (float)(oldValue - min) / (float)(max - min);
            oldValueRect.Width = (int)((float)oldValueRect.Width * percent);

            Rectangle updateRect = new Rectangle();

            // Find only the part of the screen that must be updated.
            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;

            // Invalidate the intersection region only.
            this.Invalidate(updateRect);
        }
    }

    public Color ProgressBarColor
    {
        get
        {
            return BarColor;
        }

        set
        {
            BarColor = value;

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

    private void Draw3DBorder(Graphics g)
    {
        int PenWidth = (int)Pens.White.Width;

        g.DrawLine(Pens.DarkGray,
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
        g.DrawLine(Pens.DarkGray,
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
        g.DrawLine(Pens.White,
            new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth),
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
        g.DrawLine(Pens.White,
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top),
            new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
John Wick
  • 169
  • 1
  • 13
  • Start at MAX and progress to MIN. – JohnG Jan 04 '17 at 10:46
  • where to modify in my code – John Wick Jan 04 '17 at 10:52
  • I don’t know where to modify “YOUR” code because I haven’t seen your code. I simply meant that instead of progressing from left (MIN) to right (MAX), progress from right (MAX) to left (MIN). Assuming your code controls when the progress gets updated, then simply adjust the code to decrement the current value. The code you posted is simply a link to a MS support site. You need to show what you have tried before people can help. You should view the following…[How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – JohnG Jan 04 '17 at 11:01

1 Answers1

1

In above code the progress rectangle created this way:

float percent = (float)(val - min) / (float)(max - min);
Rectangle rect = this.ClientRectangle;
rect.Width = (int)((float)rect.Width * percent);

You can simply change the code to below code, to fill the progress rectangle from right side of the control:

float percent = (float)(val - min) / (float)(max - min);
Rectangle rect = this.ClientRectangle;
var w = (int)((float)rect.Width * percent);
rect.X = rect.Width - w;
rect.Width = w;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • **Note 1:** As a general solution to convert LTR coordinates to RTL, you can use `GetRTLCoordinates` from [this post](http://stackoverflow.com/a/34509304/3110834). – Reza Aghaei Jan 04 '17 at 20:00
  • **Note 2:** As another general solution to mirror drawing, you can use a transform matrix this way `e.Graphics.Transform = new Matrix(-1, 0, 0, 1, Width, 0);` and then perform drawing and then reset the transform using `e.Graphics.ResetTransform();` – Reza Aghaei Jan 04 '17 at 20:03
  • protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush brush = new SolidBrush(BarColor); float percent = (float)(val - min) / (float)(max - min); Rectangle rect = this.ClientRectangle; var w = (int)((float)rect.Width * percent); rect.X = rect.Width - w; rect.Width = w; g.FillRectangle(brush,rect); Draw3DBorder(g); brush.Dispose(); g.Dispose(); } Notihing happens What else to do ? – John Wick Jan 05 '17 at 04:41
  • This is because of `Value` property, the code invalidates just updated parts. You can correct coordinates the same way or as a simple alternative you can simply fix it by removing all code after `// Invalidate only the changed area.` in Value` property and replace it by `this.Invalidate();`. – Reza Aghaei Jan 05 '17 at 04:52
  • one small query as u can see limit is between 0 to 100 can I make for min and max value as negative for ex. -100 to 50 , -50 to 0 – John Wick Jan 05 '17 at 04:54
  • It would be easier if you keep values from 0 to x in the progressbar, but if you want to mimic `-a` to `b`, then set the `Minimum` of progressbar to `0`, set its `Maximum` to `b+a`. Then when you want to show the value `v` which is between `-a` and `b` set the `Value` to `v+a`. – Reza Aghaei Jan 05 '17 at 05:04
  • can you please show it in code where to modify....as u know bad in dot net – John Wick Jan 05 '17 at 05:44
  • It's not code of the control. I suppose you have some values somewhere which you want to show in progressbar. So I guided you to how to show them in progressbar. – Reza Aghaei Jan 05 '17 at 05:46
  • Let me know if you have any question about original post or if you find it useful :) – Reza Aghaei Jan 05 '17 at 05:47
  • It is useful Thanks for ur help....I want to do it in code itself rather manipulating value.If wanted can raise another question – John Wick Jan 05 '17 at 05:53
  • You're welcome :) If I were you I would prefer to not touch the control code and instead manipulate my model values and then show them in control. – Reza Aghaei Jan 05 '17 at 05:55
  • ok so userControl11.Value = Convert.ToInt32(textBox1.Text) + (-100); userControl11.Maximum = 0 + (-100); As minimum = -100 and maximum = 0 and actual minimum = 0 and actual maximum = 100 right – John Wick Jan 05 '17 at 06:00
  • What's range of your model value? – Reza Aghaei Jan 05 '17 at 06:02
  • its user definable coming from textbox – John Wick Jan 05 '17 at 06:03
  • So if user defined x as minimum and y as maximim, you set 0 as `userControl.Minimum` and `y-x` as `userControl.Maximum`. Then if they want to set the value to `z` which is between `x` and `y`, then you set `userControl.Value = z - x`. Also if you want to know the model value from `userControl.Value`, then the model value is `useControl.Value + x`. – Reza Aghaei Jan 05 '17 at 06:08
  • By the way, now based on the existing code you have enough knowledge to draw the control yourself, it's not too hard :) – Reza Aghaei Jan 05 '17 at 06:09