0

is there a way to write a default text on a progressbar in C# ?

this isn't working in Form_Load, but works fine on button click...

using (Graphics gr = progressBar1.CreateGraphics())
{
    StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
    sf.Alignment = StringAlignment.Center;
    gr.DrawString("hello world", 
                  new Font("Arial", 10.0f, FontStyle.Regular),
                  new SolidBrush(Color.Black), 
                  progressBar1.ClientRectangle, 
                  sf);                
}

thanks in advance

BaKeMoNo
  • 1
  • 1
  • I would try putting a `Label` on top of the `ProgressBar` and update `Label.Text` together with `ProgressBar.Progress`. I've never done it myself, so I can't guarantee anything. – Nolonar Apr 18 '13 at 10:20

1 Answers1

0

You can set up a one time Timer event to do this on as one option.

Timer t = new Timer();

Form_Load()
{
    t.Interval = 1000;
    t.Start();
}

static void t_Elapsed(object sender, ElapsedEventArgs e)
{
    //One time event
    t.Stop();
    using (Graphics gr = progressBar1.CreateGraphics())
    {
        StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
        sf.Alignment = StringAlignment.Center;
        gr.DrawString("hello world", 
            new Font("Arial", 10.0f, FontStyle.Regular),
            new SolidBrush(Color.Black), 
            progressBar1.ClientRectangle, 
            sf);                
    }
}

One note on this:

As the ProgressBar.Value is changed; the ProgressBar graphics will change and overwrite the text you have drawn. The comment by Nolonar to overlay the Progressbar with a Label and set it in front of the ProgressBar may be be more to your needs.

Otherwise, you'd have to handle the redrawing of the ProgressBar as the graphics change.

jordanhill123
  • 4,142
  • 2
  • 31
  • 40
  • @ByteBlast 1 millisecond maybe better? :P – jordanhill123 Apr 18 '13 at 10:19
  • the label in front of the progressbar is ugly because i can't set the background to transparent (i need to change the color of the progressbar, depending on which directory i am, so no VisualStyles) – BaKeMoNo Apr 18 '13 at 13:06