| | |
How to draw a line in a VB.NET form?
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2004
Posts: 27
Reputation:
Solved Threads: 0
There are lot more capabilities in VB.NET than in VB6 -
check out:
http://msdn.microsoft.com/library/de...LineOnForm.asp
check out:
http://msdn.microsoft.com/library/de...LineOnForm.asp
•
•
Join Date: May 2005
Posts: 508
Reputation:
Solved Threads: 19
VB.NET Syntax (Toggle Plain Text)
Here is the old VB6 Way Private Sub DrawLine() 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, 3) Me.CreateGraphics.DrawLine(myPen, 0, 0, Me.Width, Me.Height) End Sub
Here is the C# .NET way
VB.NET Syntax (Toggle Plain Text)
protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics ; Pen pn = new Pen( Color.Blue ); // Rectangle rect = new Rectangle(50, 50, 200, 100); Point pt1 = new Point( 30, 30); Point pt2 = new Point( 110, 100); g.DrawLine( pn, pt1, pt2 ); }
Here is the VB .NET way
VB.NET Syntax (Toggle Plain Text)
' True while we are drawing the new line. Private m_Drawing As Boolean ' Buffer for erasing rubberband lines. Private m_BufferBitmap As Bitmap Private m_BufferGraphics As Graphics ' The mouse position. Private m_X1 As Integer Private m_Y1 As Integer Private m_X2 As Integer Private m_Y2 As Integer ' Start drawing a rubberband line. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _ As System.Windows.Forms.MouseEventArgs) Handles _ MyBase.MouseDown ' Do nothing if this isn'tthe left mouse button. If e.Button <> MouseButtons.Left Then Exit Sub m_Drawing = True ' Save a snapshot of the form. SaveSnapshot() ' Save the current mouse position. m_X1 = e.X m_X2 = e.X m_Y1 = e.Y m_Y2 = e.Y End Sub
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
hi guys!
can anyone tell me now to draw a line in VB.NET form?
I've been trying to do so , but nothings coming..
Hi there,
i was fetching the same thing, it about using the mouse down and mouse up and mouse move events on the form, you have to start drawing on mouse down and stop on mouse up, here is the code you have to put in a new form:
' True while we are drawing the new line.
Private m_Drawing As Boolean
' Buffer for erasing rubberband lines.
Private m_BufferBitmap As Bitmap
Private m_BufferGraphics As Graphics
' The mouse position.
Private m_X1 As Integer
Private m_Y1 As Integer
Private m_X2 As Integer
Private m_Y2 As Integer
' Start drawing a rubberband line.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
' Do nothing if this isn'tthe left mouse button.
If e.Button <> MouseButtons.Left Then Exit Sub
m_Drawing = True
' Save a snapshot of the form.
SaveSnapshot()
' Save the current mouse position.
m_X1 = e.X
m_X2 = e.X
m_Y1 = e.Y
m_Y2 = e.Y
End Sub
Private Sub SaveSnapshot()
Dim new_bitmap As Bitmap
' Make a new bitmap that fits the form.
new_bitmap = New Bitmap(Me.Size.Width, Me.Size.Height, Me.CreateGraphics())
m_BufferGraphics = Graphics.FromImage(new_bitmap)
' Clear the new bitmap.
m_BufferGraphics.Clear(Me.BackColor)
' Copy the existing bitmap's contents into
' the new bitmap.
If Not (m_BufferBitmap Is Nothing) Then
m_BufferGraphics.DrawImage(m_BufferBitmap, 0, 0)
End If
' Save the new bitmap and graphics objects.
m_BufferBitmap = new_bitmap
End Sub
' Continue drawing the rubberband line.
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
' Do nothing if we're not drawing.
If Not m_Drawing Then Exit Sub
' Save the new point.
m_X2 = e.X
m_Y2 = e.Y
' Erase the previous line.
DrawForm(Me.CreateGraphics())
' Draw the new line directly on the form.
Me.CreateGraphics().DrawLine( _
Pens.Gray, m_X1, m_Y1, m_X2, m_Y2)
End Sub
' Redraw the saved buffer.
Private Sub DrawForm(ByVal gr As Graphics)
If Not (m_BufferBitmap Is Nothing) Then gr.DrawImage(m_BufferBitmap, 0, 0)
End Sub
' Finish drawing the new line.
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
' Do nothing if we're not drawing.
If Not m_Drawing Then Exit Sub
m_Drawing = False
' Save the new point.
m_X2 = e.X
m_Y2 = e.Y
' Draw the new line permanently on the buffer.
m_BufferGraphics.DrawLine( _
Pens.Blue, m_X1, m_Y1, m_X2, m_Y2)
' Redraw to show the new line.
DrawForm(Me.CreateGraphics())
End Sub
' Redraw the form.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
DrawForm(e.Graphics)
End Sub
hope it work for you,
![]() |
Similar Threads
- Printing of a Form in VB.NET? (VB.NET)
- Draw a Line or other Shape (Design Time) (VB.NET)
- How to draw this line? (C++)
- How to draw a line (Java)
- Porting asp to asp.NET (with fileup) (ASP.NET)
Other Threads in the VB.NET Forum
- Previous Thread: DateTimePicker UpDown Add Only 30 Minutes
- Next Thread: loop error identifer expected
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2008 access add application arithmetic array assignment basic binary box button buttons click code combo combobox component connectionstring convert cpu data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees excel exists firewall folder image images isnumericfuntioncall listview login map math memory mobile module ms mssqlbackend mysql navigate net number opacity peertopeervideostreaming picturebox1 picturebox2 port print printpreview problemwithinstallation project record regex reports" reuse right-to-left save savedialog search serial socket sqldatbase storedprocedure string temp textbox timer txttoxmlconverter updown useraccounts usercontol usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vbnet view vista visual visualbasic visualbasic.net visualstudio web wpf xml






