Click here to Skip to main content
15,898,938 members
Home / Discussions / C#
   

C#

 
GeneralIt Works! PinPopular
Roger Wright21-May-10 18:33
professionalRoger Wright21-May-10 18:33 
GeneralRe: It Works! Pin
Luc Pattyn21-May-10 22:36
sitebuilderLuc Pattyn21-May-10 22:36 
GeneralRe: It Works! Pin
Roger Wright22-May-10 4:20
professionalRoger Wright22-May-10 4:20 
GeneralRe: It Works! Pin
Luc Pattyn22-May-10 4:30
sitebuilderLuc Pattyn22-May-10 4:30 
GeneralRe: It Works! Pin
DaveyM6921-May-10 22:46
professionalDaveyM6921-May-10 22:46 
GeneralRe: It Works! Pin
Roger Wright22-May-10 4:23
professionalRoger Wright22-May-10 4:23 
GeneralRe: It Works! Pin
Roger Wright27-May-10 16:55
professionalRoger Wright27-May-10 16:55 
GeneralRe: It Works! Pin
DaveyM6928-May-10 8:47
professionalDaveyM6928-May-10 8:47 
Hi Roger,

You're nearly there! Firstly a few comments/points...

1. Dispose of anything that is disposable if you are creating it yourself. In your code above dc and bluePen should both be disposed as you have created them.

1a. If the system creates them for you (i.e. if you used Pens.Blue instead) then don't dispose!
1b. If you are storing them in class member variables for use elsewhere, make sure they are disposed of in the class's Dispose method instead.
1c. If 1a and 1b don't apply, wrapping in a using block automatically disposes for you:
C#
using (Pen bluePen = new Pen(Color.Blue, 3))
{
    // ...
}// bluePen is now out of scope and Dispose has automatically been called!


2. Drawing anywhere but in OnPaint is sure to create you problems. The window/control will repaint for many reasons - if another window is moved over or from the top, restore from minimized, resized etc... If you are drawing in the contructor, that is never going to get called when such things happen so your drawing will disappear! Also, creating your own Graphics is an expensive operation, OnPaint supplies you one for free! OnPaint gets called automatically when any repainting is required. You can force it by calling Invalidate(); as well.

To get yourself an OnPaint method, override the existing one. The free Graphics object is available as e.Graphics. So the code in your constructor can be moved and becomes:
C#
protected override void OnPaint(PaintEventArgs e)
{
    using (Pen bluePen = new Pen(Color.Blue, 3))
    {
        e.Graphics.DrawRectangle(bluePen, 170, 10, 100, 100);
    }
    base.OnPaint(e);
}

If you need any more help, post back. Always happy to help if I can Thumbs Up | :thumbsup:
Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

GeneralRe: It Works! Pin
Roger Wright28-May-10 15:45
professionalRoger Wright28-May-10 15:45 
GeneralRe: It Works! Pin
Mohsiul Haque21-May-10 22:52
Mohsiul Haque21-May-10 22:52 
GeneralRe: It Works! Pin
Ravi Bhavnani22-May-10 6:05
professionalRavi Bhavnani22-May-10 6:05 
GeneralRe: It Works! Pin
PIEBALDconsult22-May-10 8:18
mvePIEBALDconsult22-May-10 8:18 
QuestionC # Pin
Make Up Forever21-May-10 14:27
Make Up Forever21-May-10 14:27 
AnswerRe: C # Pin
Dr.Walt Fair, PE21-May-10 14:55
professionalDr.Walt Fair, PE21-May-10 14:55 
GeneralRe: C # Pin
Make Up Forever21-May-10 15:11
Make Up Forever21-May-10 15:11 
GeneralRe: C # Pin
AspDotNetDev21-May-10 15:31
protectorAspDotNetDev21-May-10 15:31 
GeneralRe: C # Pin
Dr.Walt Fair, PE21-May-10 15:34
professionalDr.Walt Fair, PE21-May-10 15:34 
AnswerRe: C # Pin
Wes Aday21-May-10 17:05
professionalWes Aday21-May-10 17:05 
QuestionError Icomparer interface - To sort FileInfo Pin
quanvt21-May-10 13:55
quanvt21-May-10 13:55 
AnswerRe: Error Icomparer interface - To sort FileInfo Pin
Luc Pattyn21-May-10 14:20
sitebuilderLuc Pattyn21-May-10 14:20 
AnswerRe: Error Icomparer interface - To sort FileInfo Pin
AspDotNetDev21-May-10 15:39
protectorAspDotNetDev21-May-10 15:39 
GeneralRe: Error Icomparer interface - To sort FileInfo Pin
quanvt21-May-10 17:43
quanvt21-May-10 17:43 
Question.TableName, why use that? Pin
mprice21421-May-10 11:10
mprice21421-May-10 11:10 
AnswerRe: .TableName, why use that? Pin
Pete O'Hanlon21-May-10 12:29
mvePete O'Hanlon21-May-10 12:29 
AnswerRe: .TableName, why use that? Pin
PIEBALDconsult21-May-10 14:29
mvePIEBALDconsult21-May-10 14:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.