473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 30773
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
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
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
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
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
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
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
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
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.........
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.