Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Moving images inside a picturebox

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 62
Run a batch file when a Crystal Report is opened 5 47
start a process from a service 3 31
Passing data between Forms 3 20
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

790 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