Solved

Moving images inside a picturebox

Posted on 2006-06-25
3
1,638 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now