?
Solved

Moving images inside a picturebox

Posted on 2006-06-25
3
Medium Priority
?
2,095 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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