Click here to Skip to main content
15,916,842 members
Home / Discussions / C#
   

C#

 
GeneralRe: Collision Pin
Christian Graus11-Oct-07 12:57
protectorChristian Graus11-Oct-07 12:57 
GeneralRe: Collision Pin
Patrick Etc.11-Oct-07 14:44
Patrick Etc.11-Oct-07 14:44 
GeneralRe: Collision Pin
MasterSharp11-Oct-07 15:33
MasterSharp11-Oct-07 15:33 
GeneralRe: Collision Pin
Pete O'Hanlon11-Oct-07 21:55
mvePete O'Hanlon11-Oct-07 21:55 
AnswerRe: Collision Pin
Matthew Cuba11-Oct-07 14:14
Matthew Cuba11-Oct-07 14:14 
GeneralRe: Collision Pin
MasterSharp11-Oct-07 15:37
MasterSharp11-Oct-07 15:37 
GeneralRe: Collision Pin
MasterSharp11-Oct-07 15:48
MasterSharp11-Oct-07 15:48 
GeneralCare about GDI recourcess Pin
Martin#11-Oct-07 21:57
Martin#11-Oct-07 21:57 
Hello,

You have to be carefull with the GDI objects here (memory leak)!

If you want to hold the instance of your pens for further use, you have to hold the m as local members of your class.
Same to Rectangles.

But you have to dispose (free the resourcess) them in the dispose method of your class (Form).
System Drawing.Rectangle rectangle1 = System Drawing.Rectangle.Empty;
System Drawing.Rectangle rectangle2 = System Drawing.Rectangle.Empty;
 
System.Drawing.Pen penBlack = new Pen(Color.Black, 3);
System.Drawing.Pen penRed = new Pen(Color.Red, 3); //You could also use only one pen and Change the Color like you need!
 
private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics actGraphics = e.Graphics;
    rectangle1 = new  System Drawing.Rectangle(15, 15, 200, 150);
    rectangle2 = new  System Drawing.Rectangle(35, 35, 200, 150);
    actGraphics.DrawRectangle(penBlack , rectangle1);
    actGraphics.DrawRectangle(penRed , rectangle2);
}
 
protected override void Dispose( bool disposing )
{
	if( disposing )
	{
		if(components != null)
		{
			components.Dispose();
		}
		if(penBlack !=null)
			penBlack.Dispose();
		if(penRed !=null)
			penRed.Dispose();
	}
	base.Dispose( disposing );
}


If you not want to hold the instances (not suggested in this case), you would have to dispose the pens write after usage in your Paint event. A using block will do that automaticaly.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics actGraphics = e.Graphics;
    using(System.Drawing.Pen penBlack = new Pen(Color.Black, 3))
    using(System.Drawing.Pen penRed = new Pen(Color.Red, 3))
    {
        rectangle1 = new  System Drawing.Rectangle(15, 15, 200, 150);
        rectangle2 = new  System Drawing.Rectangle(35, 35, 200, 150);
        actGraphics.DrawRectangle(penBlack , rectangle1);
        actGraphics.DrawRectangle(penRed , rectangle2);
    }
}

Hope it helps!

All the best,

Martin

GeneralRe: Care about GDI recourcess Pin
MasterSharp12-Oct-07 2:41
MasterSharp12-Oct-07 2:41 
GeneralRe: Care about GDI recourcess Pin
MasterSharp12-Oct-07 2:51
MasterSharp12-Oct-07 2:51 
QuestionResizing control Pin
binaryphantom11-Oct-07 9:53
binaryphantom11-Oct-07 9:53 
AnswerRe: Resizing control Pin
Giorgi Dalakishvili11-Oct-07 10:30
mentorGiorgi Dalakishvili11-Oct-07 10:30 
AnswerRe: Resizing control Pin
binaryphantom11-Oct-07 10:42
binaryphantom11-Oct-07 10:42 
QuestionC#, ASP.net, and Active Directory.. Pin
Dio2211-Oct-07 9:52
Dio2211-Oct-07 9:52 
AnswerRe: C#, ASP.net, and Active Directory.. Pin
VirtualVoid.NET12-Oct-07 2:03
VirtualVoid.NET12-Oct-07 2:03 
QuestionAdd to value by key in Dictionary<string, List Pin
Ryan.L. R.11-Oct-07 8:57
Ryan.L. R.11-Oct-07 8:57 
AnswerRe: Add to value by key in Dictionary<string, List Pin
kubben11-Oct-07 9:01
kubben11-Oct-07 9:01 
QuestionRetrieve EMail from Drag'n'Drop out Thunderbird Pin
baerten11-Oct-07 4:42
baerten11-Oct-07 4:42 
AnswerRe: Retrieve EMail from Drag'n'Drop out Thunderbird Pin
VirtualVoid.NET12-Oct-07 2:06
VirtualVoid.NET12-Oct-07 2:06 
QuestionUnit Conversion Method Pin
patrickjamesscott11-Oct-07 4:33
patrickjamesscott11-Oct-07 4:33 
AnswerRe: Unit Conversion Method Pin
Anthony Mushrow11-Oct-07 5:52
professionalAnthony Mushrow11-Oct-07 5:52 
GeneralRe: Unit Conversion Method Pin
patrickjamesscott11-Oct-07 6:15
patrickjamesscott11-Oct-07 6:15 
AnswerRe: Unit Conversion Method [modified] Pin
patrickjamesscott11-Oct-07 6:00
patrickjamesscott11-Oct-07 6:00 
QuestionWinForm controls Pin
half-life11-Oct-07 4:19
half-life11-Oct-07 4:19 
AnswerRe: WinForm controls Pin
Scott Dorman11-Oct-07 4:29
professionalScott Dorman11-Oct-07 4: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.