How to draw a line in a VB.NET form?

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 3
Reputation: gminhas is an unknown quantity at this point 
Solved Threads: 0
gminhas gminhas is offline Offline
Newbie Poster

How to draw a line in a VB.NET form?

 
0
  #1
May 30th, 2005
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..
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 27
Reputation: srikkanthan is on a distinguished road 
Solved Threads: 0
srikkanthan srikkanthan is offline Offline
Light Poster

Re: How to draw a line in a VB.NET form?

 
0
  #2
Jun 13th, 2005
There are lot more capabilities in VB.NET than in VB6 -

check out:
http://msdn.microsoft.com/library/de...LineOnForm.asp
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 508
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: How to draw a line in a VB.NET form?

 
0
  #3
Jun 13th, 2005
  1. Here is the old VB6 Way
  2.  
  3. Private Sub DrawLine()
  4.  
  5. Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height)
  6.  
  7. Dim g As Graphics = Graphics.FromImage(bit)
  8.  
  9. Dim myPen As Pen = New Pen(Color.Blue, 3)
  10.  
  11. Me.CreateGraphics.DrawLine(myPen, 0, 0, Me.Width, Me.Height)
  12.  
  13. End Sub

Here is the C# .NET way

  1.  
  2. protected override void OnPaint(PaintEventArgs pe)
  3. {
  4.  
  5. Graphics g = pe.Graphics ;
  6. Pen pn = new Pen( Color.Blue );
  7. // Rectangle rect = new Rectangle(50, 50, 200, 100);
  8. Point pt1 = new Point( 30, 30);
  9. Point pt2 = new Point( 110, 100);
  10. g.DrawLine( pn, pt1, pt2 );
  11.  
  12. }

Here is the VB .NET way

  1. ' True while we are drawing the new line.
  2. Private m_Drawing As Boolean
  3.  
  4. ' Buffer for erasing rubberband lines.
  5. Private m_BufferBitmap As Bitmap
  6. Private m_BufferGraphics As Graphics
  7.  
  8. ' The mouse position.
  9. Private m_X1 As Integer
  10. Private m_Y1 As Integer
  11. Private m_X2 As Integer
  12. Private m_Y2 As Integer
  13.  
  14. ' Start drawing a rubberband line.
  15. Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
  16. As System.Windows.Forms.MouseEventArgs) Handles _
  17. MyBase.MouseDown
  18. ' Do nothing if this isn'tthe left mouse button.
  19. If e.Button <> MouseButtons.Left Then Exit Sub
  20. m_Drawing = True
  21.  
  22. ' Save a snapshot of the form.
  23. SaveSnapshot()
  24.  
  25. ' Save the current mouse position.
  26. m_X1 = e.X
  27. m_X2 = e.X
  28. m_Y1 = e.Y
  29. m_Y2 = e.Y
  30. End Sub
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 4
Reputation: Tbusuk is an unknown quantity at this point 
Solved Threads: 0
Tbusuk Tbusuk is offline Offline
Newbie Poster

Re: How to draw a line in a VB.NET form?

 
0
  #4
Oct 22nd, 2008
what is the SaveSnapshot() method?????
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,142
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: How to draw a line in a VB.NET form?

 
0
  #5
Oct 22nd, 2008
there are two ways, the plain old line way, and the way using GDI and pens.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: mohd40 is an unknown quantity at this point 
Solved Threads: 0
mohd40 mohd40 is offline Offline
Newbie Poster

Re: How to draw a line in a VB.NET form?

 
0
  #6
Apr 2nd, 2009
Originally Posted by gminhas View Post
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,
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: israrkhan is an unknown quantity at this point 
Solved Threads: 0
israrkhan israrkhan is offline Offline
Newbie Poster

israr

 
0
  #7
Oct 17th, 2009
Originally Posted by gminhas View Post
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 i am php developer.if did't know such about .net.if u help me in php
then reply me
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC