2

I saw this cool thing about having a RichTextBox like a "progress bar" if we can call it like that from another window application and I was trying to do the same.

I was looking at the methods and I couldn't apply any of them on what I'm looking for, I tried to see if there was a similar question but I didn't get so lucky.

How can I accomplish the same result?

Looks like a Richtextbox to me

Based on what Jimi told me I will to explain what I need. The string inside label rappresents a timer which has to change the color in red when it reaches the end, before of that, like at 10 minutes I want it to be Yellow, using that like an alert.

The way the method is wroten doesn't let me to choose which color I want. After that, when the timer is stopped by a button, I want the label to redraws itself and makes it blank without having any kind of colors, looking like a "textbox".

Zxhammelzx
  • 23
  • 4
  • It would help us if you could show which RichTextBox you plan to use. Is it in WPF or Winforms, that might matter a bit? It looks like that particular effect is maybe easier to accomplish with a Label and some Panels. Are you sure a RichTextBox is what you need? – rene Jul 22 '18 at 13:17
  • 1. Most probably a custom implemented control... did you try something, could you please edit the question and add your source code? 2. Did you see this? https://stackoverflow.com/questions/3529928/how-do-i-put-text-on-progressbar – sɐunıɔןɐqɐp Jul 22 '18 at 13:18
  • You can do it with anything you can paint on. Could be a Label or a Panel. – Jimi Jul 22 '18 at 13:19
  • IMO iIt's a picture box Control with drawing, the % is just a label inside. – Jeremy Thompson Jul 22 '18 at 13:20
  • I'm using Winforms, sorry about that – Zxhammelzx Jul 22 '18 at 13:20

1 Answers1

3

This is a label used as a ProgressBar.
Just an example (quite raw, what I could do with the time I had), but it shows how you can paint the surface of a Control that provides a Paint() event.

It uses a Timer class to increase a value and generates a progress bar effect by calling the Label.Invalidate() method, which raises the Label's Paint event, executing whatever code you have in the label1_Paint() handler.

If you want to test it, paste this code inside a Form which contains a Button (button1) to start the Timer and a Label (label1) that generates the graphic effect.
Then assign the two events - Click() to the Button and Paint() to the Label.

This is how it looks like:

enter image description here

Timer timer;
private bool TimerStarted = false;
private float ProgressMaxValue = 100;
private float Progress = 0;
private int seconds = 0;
private int cents = 0;

private void button1_Click(object sender, EventArgs e)
{
    if (TimerStarted) { TimerStop(); return; }
    timer = new Timer();
    timer.Interval = 20;
    Progress = 0;
    seconds = 0;
    cents = 0;
    timer.Tick += (s, ev) => {
        ++Progress;
        if (Progress > ProgressMaxValue) { TimerStop(); return; }
        cents += (timer.Interval / 5);
        if (cents > 99) { cents = 0; ++seconds; }
        this.label1.Invalidate();
    };
    TimerStarted = true;
    timer.Start();
}

private void TimerStop()
{
    timer.Stop();
    timer.Dispose();
    TimerStarted = false;
}

private void label1_Paint(object sender, PaintEventArgs e)
{
    StringFormat format = new StringFormat() {
        Alignment = StringAlignment.Center,
        LineAlignment = StringAlignment.Center
    };

    e.Graphics.Clear(this.label1.BackColor);
    Rectangle rect = label1.ClientRectangle;
    rect.Inflate(-1, -1);
    e.Graphics.DrawRectangle(Pens.LimeGreen, rect);
    RectangleF ProgressBar = new RectangleF(
                new PointF(3, 3),
                new SizeF((((float)rect.Width - 3) / ProgressMaxValue) * Progress, rect.Height - 4));
    e.Graphics.FillRectangle(Brushes.YellowGreen, ProgressBar);
    e.Graphics.DrawString($"0.{seconds.ToString("D2")}.{cents.ToString("D2")}", label1.Font, Brushes.White, rect, format);
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • I really appreciate your example code man. I was trying to use it to better undestand how it works, it looks like the invalidate() method doesn't work in my project because it doesn't draw the progress bar or anything, the lable remains the same, could you please explain me why? – Zxhammelzx Jul 23 '18 at 14:25
  • 1
    @Zxhammelzx Did you bind the events handlers (`label1_Paint` and `button1_Click`) to the corresponding Controls (a `Button` and a `Label`)? The `Invalidate()` method just raises the corresponding Control's Paint event. If you copy/paste this code into a Form, the Form must have a Button and a Label (as shown in the graphic example). Then, in the Form Designer, using the Control Properties panel, switch to Events view (Lightning Bolt symbol) and assign the event to the Control. – Jimi Jul 23 '18 at 18:08
  • I was missing the "_1" at the end of the name of the event, feels bad man. You were really helpful man! Thanks a lot. – Zxhammelzx Jul 23 '18 at 19:11
  • I did that. Also, I dont't know if we go OT like that, could you please explain me how I can change what's inside the DrawnString and set a different color of the DrawRectangle, is that even possible? I'm not talking about changing them into the paint method, I'm asking if it is possible after the drowing was done. Thank you – Zxhammelzx Jul 23 '18 at 21:46
  • 1
    @Zxhammelzx This is not OT, because it's related to your OP (Original Post). Yes, it's possible. If you are building a `Custom Control` or a `UserControl` it is also probably required, to have means to specify different colors for the `Text` and the `ProgressBar` properties. I'll make an edit to show how (as soon as I have some free time). – Jimi Jul 23 '18 at 21:53
  • I updated my post and I tried to explain as clear as possible what I'm trying to accomplish, thank you again. – Zxhammelzx Jul 24 '18 at 01:45