Solved

Moving images inside a picturebox

Posted on 2006-06-25
3
1,709 Views
Last Modified: 2008-02-01
Im using the code below to move and reposition images on a picturebox. First I would click the test button to add an image or two. Next click on an image, hold down the mouse button and and move it to a new position. Problem is the image is jumpy and rarely redraws exactly where I try and place it with the mouse.

There is a main picturebox, pbImages. Each time you add an image it will add to this picturebox control inside a picturebox itself. The event listeners MouseDown and MouseUp reposition the current image, pbCurrent.

    Friend WithEvents pbImages As System.Windows.Forms.PictureBox
    ... (windows forms designer generated code)
    Private bDragingImage As Boolean = False
    Private pbCurrent As PictureBox

    Private Sub btnAddImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddImage.Click
        'add an image to the array
        Dim x As New PictureBox
        x.BackColor = Color.Black
        x.SizeMode = PictureBoxSizeMode.AutoSize
        Dim Y As Image
        Y = Image.FromFile("group_andy.gif")
        x.Image = Y
        x.Left = 10
        x.Top = 10
        Dim sq As New Square(10, 10, Y.Width, Y.Height)
        AddHandler x.MouseDown, AddressOf Me.pbMouseDown
        AddHandler x.MouseUp, AddressOf Me.pbMouseUp
        x.Tag = sq
        Me.pbImages.Controls.Add(x)
        Me.pbCurrent = x
    End Sub

    Private Sub pbMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Me.sbTitle.Text = e.X.ToString & ", " & e.Y.ToString
        If Not Me.bDragingImage Then
            Me.bDragingImage = True
            Me.pbCurrent = sender
        End If
    End Sub

    Private Sub pbMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Me.sbMessage.Text = e.X.ToString & ", " & e.Y.ToString
        Me.pbCurrent.Top = e.X '+ Me.pbImages.Top
        Me.pbCurrent.Left = e.Y '+ Me.pbImages.Left
    End Sub
0
Comment
Question by:craigdev
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16978264
You need to handle also MouseMove event and change image coordinates in it, if bDragingImage is true.
All mouse event handlers should invalidate PictureBox to redraw it immediately:
pb.Invalidate
0
 
LVL 1

Author Comment

by:craigdev
ID: 16978307
At first I tried using MouseMove but I have simplified it just to try and get it working like this. I am unsure why it is jumping around all over the place.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16979871
Use...

    Private Sub btnAddImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddImage.Click
        Dim x As New PictureBox
        ' ...
        AddHandler x.MouseDown, AddressOf Me.pbMouseDown
        AddHandler x.MouseMove, AddressOf Me.pbMouseMove
        AddHandler x.MouseUp, AddressOf Me.pbMouseUp
        ' ...        
    End Sub

    Private startX, startY As Integer

    Private Sub pbMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            startX = e.X
            startY = e.Y
        End If
    End Sub

    Private Sub pbMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            sender.Location = New Point(sender.Left + e.X - startX, sender.Top + e.Y - startY)
        End If
    End Sub

    Private Sub pbMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' do something in here if you need to...
        ' but it's not necessary for the moving of the PictureBox
    End Sub
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question