Author Topic: Graphics.DrawLine on Panel  (Read 3353 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Graphics.DrawLine on Panel
« on: October 09, 2008, 03:59:28 PM »
I can't seem to draw a line on a panel on my form.  I have read he MSDN site, and got some code that they claim to work, but it won't work for me.  And the other sites that I have seen, seem to do it the say way.  Why can't I get this to work?

Thanks in advance.

Code: [Select]
using ( Graphics tempGphc = MPnl.CreateGraphics() ) {
tempGphc.DrawLine( new Pen( Color.Black ), tempPt.X + 90, StartY - 47, tempPt.X + 90, tempPt.Y + 47 );
}
Couldn't even get it to draw it on my form.
Code: [Select]
using ( Graphics tempGphc = this.CreateGraphics() ) {
tempGphc.DrawLine( new Pen( Color.Black ), 5, 455, 300, 455 );
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Draftek

  • Guest
Re: Graphics.DrawLine on Panel
« Reply #1 on: October 09, 2008, 04:20:59 PM »
Try making the x,y of the start point 0,0

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Graphics.DrawLine on Panel
« Reply #2 on: October 09, 2008, 04:47:33 PM »
Try making the x,y of the start point 0,0

That didn't work.  If I can't get this to work, then I will just have to use another way to separate my items.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Graphics.DrawLine on Panel
« Reply #3 on: October 09, 2008, 05:14:19 PM »
on my way out the door.. maybe something here will help?
http://www.theswamp.org/index.php?topic=20620.msg250780#msg250780

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Graphics.DrawLine on Panel
« Reply #4 on: October 09, 2008, 06:38:12 PM »
I couldn't get it to work the way I wanted it to, so I just create a group box per drawing to break up the dialog a little nicer.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

TonyT

  • Guest
Re: Graphics.DrawLine on Panel
« Reply #5 on: October 09, 2008, 08:08:53 PM »
I can't seem to draw a line on a panel on my form.  I have read he MSDN site, and got some code that they claim to work, but it won't work for me.  And the other sites that I have seen, seem to do it the say way.  Why can't I get this to work?

Thanks in advance.

Code: [Select]
using ( Graphics tempGphc = MPnl.CreateGraphics() ) {
tempGphc.DrawLine( new Pen( Color.Black ), tempPt.X + 90, StartY - 47, tempPt.X + 90, tempPt.Y + 47 );
}
Couldn't even get it to draw it on my form.
Code: [Select]
using ( Graphics tempGphc = this.CreateGraphics() ) {
tempGphc.DrawLine( new Pen( Color.Black ), 5, 455, 300, 455 );
}

Drawing must be done from the Paint event, or from an override of OnPaint() in a custom control.

When a control paints itself, it uses that event to notify you in case you want to do it yourself, and from the handler of the event you can draw using the event arguments.
« Last Edit: October 10, 2008, 01:48:44 AM by TonyT »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Graphics.DrawLine on Panel
« Reply #6 on: October 10, 2008, 11:01:05 AM »
Drawing must be done from the Paint event, or from an override of OnPaint() in a custom control.

When a control paints itself, it uses that event to notify you in case you want to do it yourself, and from the handler of the event you can draw using the event arguments.


I thought that might be the case, as a lot of the code I was looking at in my searches we using the Paint event to draw items, but the way it was worded on MSDN, I thought you could get the Graphics of a control, and use that to paint with.  Maybe I'll come back to this to try on another form, for now I got something that works.  Thanks for the info Tony.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.