Draw a Line on a windows form

E

eRic

Ok this is ridiculous but...how the heck do you draw a line on a windows
form in design mode? There used to be a toolbar for VB forms but I cannot
find it in VS.NET

thanks,
eRic
 
J

Justin Weinberg

Override onPaint on your form. Add something like this:

Dim g as Graphics = e.Graphics
Dim mylinePen As System.Drawing.Pen = New
System.Drawing.Pen(System.Drawing.Color.Black, 1!)
Dim mylinePointStart As System.Drawing.PointF = New
System.Drawing.PointF(50!, 210!)
Dim mylinePointEnd As System.Drawing.PointF = New
System.Drawing.PointF(50!, 50!)

g.DrawLine(mylinePen, mylinePointStart, mylinePointEnd)
mylinePen.Dispose
MyBase.onPaint(e)




Here's a class you can just instantiate and pass your e.Graphics event to:

'GDIPlus Architect Component Class Output
Public Class Untitled1
Inherits System.ComponentModel.Component

Private mylinePen As System.Drawing.Pen = New
System.Drawing.Pen(System.Drawing.Color.Black, 1!)

Protected mylinePointStart As System.Drawing.PointF = New
System.Drawing.PointF(50!, 210!)

Protected mylinePointEnd As System.Drawing.PointF = New
System.Drawing.PointF(50!, 50!)

Public Sub New()
MyBase.New
Me.InitializeGraphics
End Sub

Private Sub InitializeGraphics()
End Sub

Public Overridable Sub RenderGraphics(ByVal g As
System.Drawing.Graphics)
g.DrawLine(Me.mylinePen, Me.mylinePointStart, Me.mylinePointEnd)
End Sub

'Required to dispose of created resources
Private Sub DisposeGraphics()
Me.mylinePen.Dispose
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
Me.DisposeGraphics
End If
End Sub
End Class


C# version:

// GDIPlus Architect Component Class Output
public class Untitled1 : System.ComponentModel.Component {

private System.Drawing.Pen mylinePen = new
System.Drawing.Pen(System.Drawing.Color.Black, 1F);

protected System.Drawing.PointF mylinePointStart = new
System.Drawing.PointF(50F, 210F);

protected System.Drawing.PointF mylinePointEnd = new
System.Drawing.PointF(50F, 50F);

public Untitled1() {
this.InitializeGraphics();
}

private void InitializeGraphics() {
}

public virtual void RenderGraphics(System.Drawing.Graphics g) {
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawLine(this.mylinePen, this.mylinePointStart,
this.mylinePointEnd);
}

// Required to dispose of created resources
private void DisposeGraphics() {
this.mylinePen.Dispose();
}

protected override void Dispose(bool disposing) {
if (disposing) {
this.DisposeGraphics();
}
}
}
 
T

Tom Clement

It's not a ridiculous question at all. In fact it's a commonly asked
question.

In general, when you have this kind of question, you should use Google
Groups to search for the answer. You can find it at:
http://groups.google.com/

To answer your specific question, you can draw a vertical or horizontal line
by using a panel (or label) control and setting it's horizonal or vertical
size to 1. For diagnonal or curved lines, there is no control to help you
out, but you can draw it fairly easily during the Paint event (or by
overriding OnPaint()).

Here's some VB code from a post by Jay B Harlow [MVP Outlook]

The following code will draw the line on the panel.

Private Sub Panel1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Panel1.Paint
Dim BlackPen As Pen = New Pen(Color.Black, 1)
Dim Point1 As Point = New Point(5, 40)
Dim Point2 As Point = New Point(765, 40)
e.Graphics.DrawLine(BlackPen, Point1, Point2)
End Sub

The following code will draw the line on the form itself:

Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim BlackPen As Pen = New Pen(Color.Black, 1)
Dim Point1 As Point = New Point(5, 40)
Dim Point2 As Point = New Point(765, 40)
e.Graphics.DrawLine(BlackPen, Point1, Point2)
End Sub
 
E

eRic

ok great, thanks...so there is no longer a way to draw a line on a form in
design view?
all form design (aside from obvious control placement and image
addition,etc...) is now handled programatically??
 
J

Justin Weinberg

No, there isn't a way to draw on a form in design view anymore,
unfortunately. Maybe in the next release of .NET? I think there is a shape
component out there somewhere that gives design time support for drawing,
but I never used it. Might be worth doing a google search for it.

Not to plug (ok a little) - GDI+ Architect can really help you get started
with drawing to forms in .NET.if you are unfamiliar with it. Even if you
don't purchase it - you can learn a lot by downloading the 15 day free trial
and playing with it and seeing what kind of code output it produces for you.
 
A

Allen Anderson

its a bit hokey, but when I just need a regular line on a windows form
I take the picture box set its border to FixedSingle and its width or
height to 1 (giving it a single line appearance). Its not perfect but
it makes for an easy way to get a line on a form.

Allen Anderson
http://www.glacialcomponents.com
 
H

Herfried K. Wagner [MVP]

* "eRic said:
Ok this is ridiculous but...how the heck do you draw a line on a windows
form in design mode? There used to be a toolbar for VB forms but I cannot
find it in VS.NET

There are no line controls any more. The drawing tools can be used to
create bitmap files and icons and are not available in the form
designer.

Line control "replacement":

<http://download.microsoft.com/download/7/e/0/7e070297-47fe-4443-9194-ab57acd8ea01/LineControls.msi>

You can add code to the 'Paint' handler to draw a line:

\\\
Private Sub Form1_Paint( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs _
) Handles Form1.Paint
e.Graphics.DrawLine(Pens.Black, 0, 0, 100, 100)
End Sub
///
 
B

Bob Powell [MVP]

There are no controls for this that you can use to lay out graphical object,
such as lines and shapes, in design mode.

Creating one however is simplicity itself. That's what a component based
architecture is all about.

After my signature is a line control that works in the design time. If it's
wider than it is tall the line is horizontal, If taller than wide it's
vertical..

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

using System;

using System.Drawing;

using System.ComponentModel;

using System.Windows.Forms;



namespace DrawingForum

{

/// <summary>

/// Summary description for LineControl.

/// </summary>

public class LineControl : Control

{

int _lineWidth;



[Category("Appearance"),

Description("The thickness of the line in pixels")]

public int LineWidth

{

get{return _lineWidth;}

set{

_lineWidth=value;

this.Invalidate();

}

}





public LineControl()

{

this.SetStyle(

ControlStyles.SupportsTransparentBackColor |

ControlStyles.ResizeRedraw,true);

}



protected override void OnPaint(PaintEventArgs e)

{

Pen p=new Pen(this.ForeColor, this.LineWidth);

if(this.Height>this.Width)

{

e.Graphics.DrawLine(p,this.Width/2,0,this.Width/2,this.Height);

}

else

{

e.Graphics.DrawLine(p,0,this.Height/2,this.Width,this.Height/2);

}

p.Dispose();

base.OnPaint(e);

}



}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top