How to draw graphics onto a control

Post your C# code snippets in here.
4 posts Page 1 of 1
Contributors
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

How to draw graphics onto a control
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();                       
                }
          
Last edited by Bogoh67 on Sun Oct 16, 2011 8:20 am, edited 1 time in total.
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

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
http://vagex.com/?ref=25000
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

yea i knew i was forgetting something
SFont.dispose();
SBrush.dispose();

Right?
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Bogoh67 wrote:
yea i knew i was forgetting something
SFont.dispose();
SBrush.dispose();

Right?
also g.Dispose();
and maybe stringformat too but idk
http://vagex.com/?ref=25000
4 posts Page 1 of 1
Return to “Quick Snips”