Question Moving and Stretching PictureBox with Mouse

CoffeeMan

New member
Joined
Oct 16, 2008
Messages
4
Programming Experience
3-5
Hi all,

Here is an example in Flash of what I'm trying to get done in VB.NET (VS 2005): http://members.cox.net/jbranscum3/integratedSchedule11.swf

I was able to get the bars to move left and right pretty easily in Flash, however I can't seem to find much about doing this in VB.NET. I've included my currently fruitless attempts in VB as an attachment.

Even the ability to move a picturbox with the mouse at runtime would be a big help.

Thanks to anyone that knows how to do it.
 

Attachments

  • Graphics.zip
    31.3 KB · Views: 48
Last edited by a moderator:
Try this inherited Picturebox control:
VB.NET:
Public Class PBBar
    Inherits PictureBox

    Public Sub New()
        Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
    End Sub

    Private Enum Area
        Left
        Center
        Right
    End Enum
    Private dragAction As Area
    Private dragAreaLeft, dragAreaRight As Rectangle
    Private origX As Integer, origWidth As Integer

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseDown(e)
        If Me.dragAreaLeft.Contains(e.Location) Then
            Me.dragAction = Area.Left
        ElseIf Me.dragAreaRight.Contains(e.Location) Then
            Me.dragAction = Area.Right
        Else
            Me.dragAction = Area.Center
        End If
        Me.origX = e.X
        Me.origWidth = Me.Width
    End Sub

    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(e)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim diffX As Integer = e.X - Me.origX
            Select Case Me.dragAction
                Case Area.Center
                    Dim loc As Point = Me.Location
                    loc.Offset(diffX, 0)
                    Me.Location = loc

                Case Area.Right
                    Me.Width = Me.origWidth + diffX

                Case Area.Left
                    Dim loc As Point = Me.Location
                    loc.Offset(diffX, 0)
                    Me.Location = loc
                    Me.Width -= diffX                    
            End Select
        End If
    End Sub
 
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        Me.dragAreaLeft = New Rectangle(0, 0, 20, Me.Height)
        Me.dragAreaRight = New Rectangle(Me.Width - 20, 0, 20, Me.Height)
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(pe)
        pe.Graphics.FillRectangle(Brushes.Green, Me.dragAreaLeft)
        pe.Graphics.FillRectangle(Brushes.Green, Me.dragAreaRight)
    End Sub

End Class
When you have added this class to your project rebuild it and go to Toolbox, you should find the PBBar control in the "yourproject Components" area, then add it to form. In runtime you can move and stretch this bar as requested.
 
hello, how do i resize it up and down?
When you understand the code used to drag it left-right, then you will have no problem adding up-down code to it also. Study hard! ;)
 
Back
Top