Monday 10 December 2012

How do put text on ProgressBar?

Display Custom Text

using (Graphics gr = progressBar1.CreateGraphics())
{
     string sString="Progress Bar Visible";
     Brush b = new SolidBrush( Color.Red);
     StringFormat sf = new StringFormat( StringFormatFlags.NoWrap );
     sf.Alignment = StringAlignment.Center;
     gr.DrawString(sString, new Font("Arial", 12.0f,FontStyle.Bold), b,  progressBar1.ClientRectangle, sf);
     gr.Dispose();
     b.Dispose();
     sf.Dispose();
}

Add the Percent  into a Standard Progress Bar Control

 
int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) /
(double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
using (Graphics gr = progressBar1.CreateGraphics())
{
    gr.DrawString(percent.ToString() + "%",
        SystemFonts.DefaultFont,
        Brushes.Black,
        new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
            SystemFonts.DefaultFont).Width / 2.0F),
        progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
            SystemFonts.DefaultFont).Height / 2.0F)));
}
 
Download Sample Coding (Progress bar.rar) 

1 comment: