By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

How to draw a line in a Windows Form?

P: n/a

In my C# Windows Form, how do I draw a Line?
Thanks for help.

Nov 29 '05 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Jason Huang wrote:

In my C# Windows Form, how do I draw a Line?
Thanks for help.


You can do all sorts of things... the straight forward way is to use
Graphics.DrawLine. You can get the Form's graphic object by doing
something like this (I'm not checking my code, so it might be slightly
different than this):
Graphics.FromHnd( this.Handle )
then you can quickly draw a line straight from that
Graphics.FromHnd( this.Handle ).DrawLine( args to draw line with )
I'm not sure if getting a graphics object in this way requires you to
also dispose of it - if that's the case you may want to do:
using ( Graphics g = Graphics.FromHnd( this.Handle ) )
g.DrawLine( args to draw line with );
but like I said, i'm not 100% sure...

Play around with that...

For super easy lines, if you want a straight line (Horiz or Vert), you
can make a panel, change it's background color to the color you would
like the line to be, and then adjust it's width and height.

Nov 29 '05 #2

P: n/a
Change Label control's BorderStyle.
Is that u want?

Nov 29 '05 #3

P: n/a
Shapes are drawn by calling methods of a Graphics object. Most methods
require a rectangle, which is used as the bounding rectangle for the shape,
as well as a pen.

Drawing lines is accomplished with the DrawLine() method of the Graphics
object. DrawLine() is one of the few drawing methods that doesn't require a
rectangle. The syntax for DrawLine() is:
object.DrawLine(pen, x1, y1, x2, y2);

Drawing Rectangles or squares is accomplished using the DrawRectangle()
method of a Graphics object. DrawRectangle() accepts a pen and a rectangle.

object.DrawRectangle(pen, rectangle);

But, if you don't have a Rectangle object (and you don't want to create
one), you can call DrawRectangle() using the following:

object.DrawRectangle(pen, X, Y, width, height);

Drawing ellipses (that includes circles) is accomplished by calling the
DrawEllipse() method. If you're familiar with geometry, you'll know that a
circle is simply an ellipse that has the same height as it does width. The
rectangle is used as a bounding rectangle—the width of the rectangle is the
width of the ellipse, whereas the height of the rectangle is the height of
the ellipse.

object.DrawEllipse(pen, rectangle);

In the event that you don't have a Rectangle object defined (and again you
don't want to create one), you can call DrawEllipse() with this syntax:

object.DrawEllipse(pen, X, Y, Width, Height);

To clear the surface of a Graphics object, call the Clear() method, passing
it the color to paint the surface, like this:

Nov 29 '05 #4

P: n/a
> In my C# Windows Form, how do I draw a Line?

Dry Erase markers are the best. If you make a mistake, you can just wipe it
off the screen with a little alcohol.


Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

"Jason Huang" <Ja************> wrote in message

In my C# Windows Form, how do I draw a Line?
Thanks for help.


Nov 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.