473,513 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to draw a line in a Windows Form?

Hi,

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

Jason
Nov 29 '05 #1
4 30790
Jason Huang wrote:
Hi,

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

Jason


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:

objGraphics.Clear(Drawing.SystemColors.Control);
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.

--
HTH,

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
news:ek****************@TK2MSFTNGP12.phx.gbl...
Hi,

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

Jason

Nov 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
8523
by: Ed Bitzer | last post by:
Can draw a line on my form with a button click event but cannot upon the Load event when I wish. No more line object so used the following: Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height) Dim...
4
3130
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
4
5141
by: thomasp | last post by:
I found the following code on MSDN to draw a line in VB2005. Public Sub DrawLinePoint(ByVal e As PaintEventArgs) ' Create pen. Dim blackPen As New Pen(Color.Black, 3) ' Create points that...
1
12703
by: balakrishnan.dinesh | last post by:
Hi frnds, Im creating function to plot line graph in javascript . I have marked the points in graph. but what i need to do is, i want to draw the line between those marked point, but i dont...
5
4476
by: Ajith Menon | last post by:
I want to copy a part of the windows form into clipboard as .bmp file. The usecase is the user drags using the mouse and creates a rectangular selection any where on the form. This part has to be...
0
1325
by: phanibiddu | last post by:
How to draw on windows form and save it as Jpeg file I am trying with this code but not comming public Form3() { InitializeComponent(); //rnd = new...
7
9334
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
1200
by: joshguru | last post by:
i have to draw a line by mouse after i clicked the button "draw line" in my form in vb.net....send me sample coding.........
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.