The code that follows demonstrates the drawing of rubberbanded lines in VB2010. Tested with Windows 7.
The form setup consists of the form with a PictureBox within it. (The form code is not included, so that you will have make that yourself),
Code:
' a rubberbanded line(s) may be drawn in a PictureBox. July 2014
Public Class Form1
Dim ori As Point
Dim lastori As Point
Dim mousedwn, firstdown As Boolean
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
mousedwn = True
firstdown = True
ori.X = e.X 'origin of line
ori.Y = e.Y
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseMove
If mousedwn Then
If Not firstdown Then 'remove last line
ControlPaint.DrawReversibleLine(PictureBox1.PointToScreen(ori), _
PictureBox1.PointToScreen(lastori), SystemColors.Control)
End If
ControlPaint.DrawReversibleLine(PictureBox1.PointToScreen(ori), _
PictureBox1.PointToScreen(New Point(e.X, e.Y)), SystemColors.Control)
lastori.X = e.X
lastori.Y = e.Y
firstdown = False
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseUp
mousedwn = False
' uncomment these lines to enable a single line at a time with length and angle
' Dim endpt As Point
' endpt.X = e.X
'endpt.Y = e.Y
'Dim len As Integer
'len = Math.Sqrt((endpt.X - start.X) ^ 2 + (endpt.Y - start.Y) ^ 2) 'compute len of tape
'Dim angle As Integer = Math.Asin((start.Y - endpt.Y) / len) * 57.2958 'test line only
'MessageBox.Show("length of tape= " & CStr(len) & " angle is " & CStr(angle), "Measuring tape")
''remove line
'ControlPaint.DrawReversibleLine(PictureBox1.PointToScreen(start), _
' PictureBox1.PointToScreen(endpt), SystemColors.Control)
End Sub
End Class
The program will let you draw any number of lines or a single line with length and angle info. See MouseUp event to change behavior.
Good luck.