471,063 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,063 software developers and data experts.

How to draw a line in a Windows Form?


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

Nov 29 '05 #1
4 30574
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
Change Label control's BorderStyle.
Is that u want?

Nov 29 '05 #3
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
> 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************@hotmail.com> 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.

Similar topics

18 posts views Thread by Ed Bitzer | last post: by
4 posts views Thread by Rod Gill | last post: by
4 posts views Thread by thomasp | last post: by
1 post views Thread by balakrishnan.dinesh | last post: by
5 posts views Thread by Ajith Menon | last post: by
reply views Thread by joshguru | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.