Page 1 of 1

How to draw graphics onto a control

Posted: Sun Oct 16, 2011 5:18 am
by Bogoh67
Lets say for instance you have a webbrowser and you want the webbrowser url in the progressbar without using labels this is how you do it

Add a progressbar first of course
and then add a timer enable it and have it have an interval of 1
then double click it and put
Code: Select all
 {
                    Font SFont = new Font("Courier New", 8);
                    SolidBrush SBrush = new SolidBrush(Color.Black);
                    Graphics g = progressBar1.CreateGraphics();
                    StringFormat SFormat = new StringFormat();
                    g.DrawString(Webbrowser1.url, SFont, SBrush, 5, 5, SFormat);
                    SFont.Dispose();
                    SBrush.Dispose();
                    g.Dispose();                       
                }
          

Re: How to draw graphics onto a control

Posted: Sun Oct 16, 2011 8:08 am
by Axel
A progressbar has a paint event so you can just put the code into the paint.
By the way, if you create Graphics g over and over again it will cause a memory leak. You should dispose it or create it once outside the timer

Re: How to draw graphics onto a control

Posted: Sun Oct 16, 2011 8:14 am
by Bogoh67
yea i knew i was forgetting something
SFont.dispose();
SBrush.dispose();

Right?

Re: How to draw graphics onto a control

Posted: Sun Oct 16, 2011 8:15 am
by Axel
Bogoh67 wrote:
yea i knew i was forgetting something
SFont.dispose();
SBrush.dispose();

Right?
also g.Dispose();
and maybe stringformat too but idk