Link to home
Start Free TrialLog in
Avatar of yaron89
yaron89

asked on

User control of picturebox location

What is the best way to enable the user changing the location of picturebox or buttons on a form?
Avatar of wht1986
wht1986
Flag of United States of America image

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?
Avatar of yaron89
yaron89

ASKER

I mean dragging it to a new location on the form.
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

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yaron89

ASKER

Thank You