• 0

[C#] Text on progress bar problem


Question

Im wanting to put text (the %) on a progress bar, tried a lable but they dnt have proper transparency, then tried drawing the text by using the creatgraphics method of the progress bar, then the drawtext method,

this works except the text is always there, i write say 5% then the next time i write 10% but the 5% is still behind it, i tried clearing it with clear method but this also clears the whole progressbar, how can i just clear the text drawn last time?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

You need to invalidate the progress bar, too.

 protected override void OnPaint(PaintEventArgs e)
  {
 	 base.OnPaint (e);
 	 int tmp = progressBar1.Value;
 	 double pct = (double)tmp/(double)progressBar1.Maximum;

 	 string spct = string.Format( "{0:P}", pct );

 	 Brush b = new SolidBrush( Color.White );
 	 Graphics g = Graphics.FromHwnd( progressBar1.Handle );

        /**************************************/
        // Invalidate progress bar
 	 progressBar1.Invalidate();

 	 StringFormat sf = new StringFormat( StringFormatFlags.NoWrap );
 	 sf.Alignment = StringAlignment.Center;
 	 g.DrawString( spct, new Font( "Arial", 12.0f ), b,
    progressBar1.ClientRectangle, sf );

 	 g.Dispose();
 	 b.Dispose();
 	 sf.Dispose();
  }

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.