Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

User control of picturebox location

Posted on 2009-05-05
5
Medium Priority
?
424 Views
Last Modified: 2013-12-17
What is the best way to enable the user changing the location of picturebox or buttons on a form?
0
Comment
Question by:yaron89
[X]
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
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24304995
do you mean changing the location of the image in a picture box, e.g. specifying the image displayed is c:\temp\pic1.gif? or do you mean, the picture box should now be at a new location, e.g. 200 pixels from the top and 300 pixels from the left on the form?
0
 

Author Comment

by:yaron89
ID: 24306462
I mean dragging it to a new location on the form.
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24307017
You can use the mouse up/down.move events like below
private bool isDragging = false;
        private int currentX, currentY;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // load from some config file if needed for last set position
        }
        
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isDragging = true;
            currentX = e.X;
            currentY = e.Y;
        }
 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                this.pictureBox1.Top = this.pictureBox1.Top + (e.Y - currentY);
                this.pictureBox1.Left = this.pictureBox1.Left + (e.X - currentX);
            }
        }
 
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
            // save the current values to some config file if needed
        }

Open in new window

0
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 total points
ID: 24307061
to make it easy to support multiple picture boxes, i would suggest creating your own class like below. then you can as many on the form as you need
public class MyDraggablePictureBox : PictureBox
{
    private bool isDragging = false;
    private int currentX, currentY;
 
    protected override void OnMouseDown(MouseEventArgs e)
    {
        isDragging = true;
        currentX = e.X;
        currentY = e.Y;
        base.OnMouseDown(e);
    }
 
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (isDragging)
        {
            this.Top = this.Top + (e.Y - currentY);
            this.Left = this.Left + (e.X - currentX);
        }
        base.OnMouseMove(e);
    }
 
    protected override void OnMouseUp(MouseEventArgs e)
    {
        isDragging = false;
        base.OnMouseUp(e);
    }
}

Open in new window

0
 

Author Closing Comment

by:yaron89
ID: 31577985
Thank You
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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

715 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