Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a line "symbiote" to help users draw rubberband lines in VB .NET
DescriptionThis example shows how to make a line "symbiote" to help users draw rubberband lines in Visual Basic .NET.
Keywordsline symbiote, rubberband line, draw, VB.NET
CategoriesGraphics, VB.NET
 
This example uses a component that I call a symbiote. It works in conjunction with a PictureBox to let the user draw a rubberband line.

The class inherits from Component so it is displayed in the component tray at design time and is invisible at run time.

The Canvas property gives the control on which the symbiote draws. Its m_Canvas variable is declared WithEvents so the control can receive its events easily. The main program should set this property to a control when it wants to let the user select a rubberband line and it should set this to Nothing to prevent the user from selecting a line.

 
' The PictureBox on which we will draw.
Private WithEvents m_Canvas As Control

' The control on which we will draw.
Public Property Canvas() As Control
    Get
        Return m_Canvas
    End Get
    Set(ByVal Value As Control)
        m_Canvas = Value
    End Set
End Property
 
The MouseDown, MouseMove, and MouseUp event handlers draw the line. When the control needs to clear the previous line, it raises the Redraw event. The main program should redraw the PictureBox.

When the user releases the mouse, the control raises its LineSelected event.

 
Public Event Redraw()
Public Event LineSelected(ByVal x0 As Single, ByVal y0 As _
    Single, ByVal x1 As Single, ByVal y1 As Single)

' Start drawing.
Private Sub m_Canvas_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Canvas.MouseDown
    If e.Button <> MouseButtons.Left Then Exit Sub
    m_Drawing = True

    ' Save the point.
    m_X0 = e.X
    m_Y0 = e.Y
    m_X1 = e.X
    m_Y1 = e.Y

    ' Draw the line.
    m_Canvas.CreateGraphics.DrawLine(Pens.Black, m_X0, _
        m_Y0, m_X1, m_Y1)
End Sub

' Continue drawing.
Private Sub m_Canvas_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Canvas.MouseMove
    If Not m_Drawing Then Exit Sub

    ' Erase the previous line.
    RaiseEvent Redraw()

    ' Save the new point.
    m_X1 = e.X
    m_Y1 = e.Y

    ' Draw the line.
    m_Canvas.CreateGraphics.DrawLine(Pens.Black, m_X0, _
        m_Y0, m_X1, m_Y1)
End Sub

' Finish drawing.
Private Sub m_Canvas_MouseUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Canvas.MouseUp
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    ' Raise the LineSelected event. 
    ' The main program can refresh the canvas if desired.
    RaiseEvent LineSelected(m_X0, m_Y0, m_X1, m_Y1)
End Sub
 
For information on building controls in Visual Basic .NET (and tons of other topics), see my book Visual Basic 2005 Programmer's References.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated