Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to draw a line in Visual Studio C# 2008 on Windows Form application !

I want draw a line with toolbox !
Help me to find .dll about it to add my toolbox then draw !
Posted
Updated 18-May-11 23:42pm
v2

You don't need an additional .dll. The ability to draw a line comes with .NET framework for free. Here is how it's done:

Draw your special line onto one of your controls.

Version 1
Attach a method to the underlying control's Paint event.
BaseControl.Paint += new PaintEventHandler(BaseControl_Paint);
The method now is called a paint event handler. It will have an argument of type PaintEventArgs, often called "e".
In your paint event handler, call
private void BaseControl_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLine(Pens.Orange, firstPoint, secondPoint);
}
An orange line will be drawn from the first point to the second. Points are in co-ordinates of the underlying control with (0,0) in the control's upper left corner.

Version 2
Subclass the underlying control and override OnPaint:
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    e.Graphics.DrawLine(Pens.Orange, firstPoint, secondPoint);
}
It's important that you call your DrawLine() after base.OnPaint() in order to draw on top of the underlying control. Otherwise your line would get painted over and never been seen.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-11 13:41pm    
Pretty good, but OnPaint is better for form as it is already inheriting from the Form class. 5 anyway.

There is more to it, please see my answer.
At least I explain why PictureBox (many mistakenly use) is really bad.
--SA
Personally, I would handle the Paint event for the form, and draw it there.
But, if you must draw it in the toolbox: look at the LineShape[^] control from the Visual Basic Powerpacks[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-11 13:43pm    
On Paint is better because the use form is already inheriting from the Form class. My 4.
There is more to it.

Please see my answer. At least I explain why not PictureBox (some advice) is really bad.
--SA
You may draw a line, programmatically, using the Graphics.DrawLine[^] method.
 
Share this answer
 
Please see my detailed answers to past questions below. I talk about controls, but your Form is a Control. I still recommend to draw on some child control like Panel or your own custom control, but Form would certainly work.

See:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How do I clear a panel from old drawing[^].

—SA
 
Share this answer
 
a
><&<a href=""></a><a href=""></a>[<a href="" target="_blank"></a>]<big><small></small></big>
 
Share this answer
 
v2
Hi,

Have a look at the following posts:

How to: Draw a Line on a Windows Form[^]
Drawing with Graphics in WinForms using C#[^]

Remember Google is your friend....

Kind regards,
 
Share this answer
 
 
Share this answer
 
You can drag a PictureBox from Toolbox on to your Form. Make it 1 or 2 px hight and black. And do not assign any image to it.
 
Share this answer
 
Comments
yesotaso 19-May-11 16:02pm    
Hmm of course you can use axe to slice bread instead of kitchen knife :)
Thats not my 1...
Kim Togo 20-May-11 1:07am    
Just like firefighting. Turn off the fire with power force :-)
I'm going to migrating from VB6 to C# .NET; I can't draw a line or shape on the form at design time ?! O_O, it's a joke?.

Pls
 
Share this answer
 
Comments
[no name] 18-Apr-12 22:50pm    
And you just had to resurrect a 1 year old question with this asinine comment why?
Member 7921268 19-Apr-12 6:51am    
'yes' or 'nope' it would have been sufficient.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900