473,782 Members | 2,439 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 30825
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.DrawLi ne. 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.FromHn d( this.Handle )
then you can quickly draw a line straight from that
Graphics.FromHn d( 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.FromHn d( 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.DrawRect angle(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.DrawRect angle(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.DrawElli pse(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.DrawElli pse(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.Cle ar(Drawing.Syst emColors.Contro l);
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******** ********@TK2MSF TNGP12.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
8572
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 g As Graphics = Graphics.FromImage(bit) Dim myPen As Pen = New Pen(Color.Blue, 1) Me.CreateGraphics.DrawLine(myPen, 0, 5, Me.Width, 5) Appreciate some help. Ed
4
3160
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 further away, completely leaving the selector box area. Any ideas? VS 2003 and VB.Net This is a simple application at the moment but the form is inherited from a
4
5160
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 define line. Dim point1 As New Point(100, 100) Dim point2 As New Point(500, 100)
1
12721
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 konw how to draw line betwwen the marked points The output for my function will be like this , So the marked point (*) should be connected through lines,
5
4501
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 copied into clipboard. Can anybody help me in implementing this using C#? Thanks, Ajith
0
1344
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 Random((int)DateTime.Now.Ticks); // seeded with ticks myPen = new Pen(Color.Red);
7
9372
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 display the Windows form. The form is being displayed but the command only returns when the form is closed. I want the command line to return immediately, leaving the form displayed.
0
1228
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
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.