Reputation: 23
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".
Upvotes: 2
Views: 111
Reputation: 32288
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:
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);
}
Upvotes: 3