Multi Selection in PictureBox

yuzhen

Member
Joined
May 13, 2009
Messages
12
Programming Experience
Beginner
hi,

I'm currently working on multi selection on the Picturebox. I'm kinda of stuck with these coding. can anyone pls help me. thks. :(

VB.NET:
Public Class Page_2
[COLOR="seagreen"]' for selection on part of body[/COLOR]
    Private start As Point = Point.Empty
    Private rect() As Rectangle
    Private p As Pen
    Dim CurSel As String [COLOR="seagreen"](what shld i DIM it as??)[/COLOR]

Private Sub Page_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'initialize pen
        p = New Pen(Color.Black)
        p.DashStyle = Drawing2D.DashStyle.Dash

    End Sub

Private Sub pbBody_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown


        start = e.Location

    End Sub

    Private Sub pbBody_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseMove

        If start <> Point.Empty Then
            Dim x As Integer = Math.Min(start.X, e.X)
            Dim y As Integer = Math.Min(start.Y, e.Y)
            Dim wid As Integer = Math.Abs(start.X - e.X)
            Dim hgt As Integer = Math.Abs(start.Y - e.Y)
            Dim changefilter As Integer = 5
            If Math.Abs(x - wid) > changefilter _
              OrElse Math.Abs(y - Height) > changefilter Then
                CurSel += 1 ' Increase counter
                [COLOR="seagreen"]' Redim the array[/COLOR]
                ReDim Preserve rect(CurSel)
                rect(CurSel) = New Rectangle(x, y, wid, hgt)
                [COLOR="seagreen"]' At this point you may call PackData function to see if the new selection data "fits".
                ' If it doesn't, "rollback" this new item.
                ' A better way may be to "disable" selection feature if the user has made too many selections[/COLOR]                
            pbBody.Refresh()
            End If
        End If
    End Sub


    Private Sub pbBody_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp

        start = Point.Empty

    End Sub

    Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint
        ReDim Preserve rect(CurSel)
        If rect(CurSel) <> Rectangle.Empty Then e.Graphics.DrawRectangle(p, rect(CurSel))

    End Sub

    Private Sub menuUndo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuUndo.Click

        If CurSel >= 0 Then
            rect(CurSel) = Rectangle.Empty
            If CurSel >= 0 Then
                [COLOR="seagreen"]' Redim the array, can't redim to a negative size![/COLOR]
                ReDim Preserve rect(CurSel)
            End If
            pbBody.Refresh()
        End If

    End Sub

End Class
Attached is how it shld work

Best Regards,
Dawn.Lee:confused:
 

Attachments

  • selection area.JPG
    selection area.JPG
    43.4 KB · Views: 29
Each of those is just a rectangle drawn on the control with a specific type of pen. [ame="http://www.vbforums.com/showthread.php?t=426684"]Read this[/ame] to see how to draw multiple lines on a PictureBox. Given that each line has a bounding rectangle, it should be fairly easy to adapt that to draw rectangles instead.
 
Back
Top