Ladessa
Ladessa

Reputation: 1003

Change the position of an PictureBox

I have 2 pictureBoxes on a flowLayout Panel and need move the pictureBox1 to pictureBox2's position and pictureBox2 to pictureBox1's position

EDIT:

I have tried but the pictureBoxes dont move...I have used MessageBox for check the positions..and the positions are right but they dont swap the position(the locations not changed...)

     MessageBox.Show("PixBoxMover x " + picBoxMover.Location.X + "y " + picBoxMover.Location.Y);
        MessageBox.Show("picBoxMovendo x " + picBox.Location.X + "y " + picBox.Location.Y);

        Point temp = picBox.Location;

        picBox.Location = picBoxMover.Location;
        picBoxMover.Location = temp;

        MessageBox.Show("PixBoxMover x " + picBoxMover.Location.X + "y " + picBoxMover.Location.Y);
        MessageBox.Show("picBoxMovendo x " + picBox.Location.X + "y " + picBox.Location.Y);

Upvotes: 3

Views: 61634

Answers (6)

Ricardo Fercher
Ricardo Fercher

Reputation: 977

As I had also very big problems with moving a PictureBox in a certain Interval I decided to Code a useful function. The function get called every Tick of the Timer. The Sense of the function is to move a picturebox starting from (0 + Offset) until (end_point_of_movement + Offset). Maybe the function is useful for some of you:

    private void timer_analytics_Tick(object sender, EventArgs e)
    {

        //This function is used to move a PictureBox in a certain Interval inclusive Offset 

         int interval, intervalOffset, xCoo, yCoo, xCooNew;
         interval = 303 - 247;                              //Max min location picture
         intervalOffset = 247;                              //Starting Point of PictureBox
         xCoo = pictureBox_GreenArrow.Location.X;           //get xCoordinate of PictureBox
         xCoo -= intervalOffset;                            //Substract Offset in order to calculate next Point
         yCoo = pictureBox_GreenArrow.Location.Y;           //get yCoordinate of PictureBox
         xCooNew = ((xCoo + 1) % (interval));               //calculate new xCoordinate of PictureBox
                                                            //the inverval runs automatically from 0 to (303-247)

        //set new Point of PictureBox which goes from (0 + intervalOffset) until (interval + intervalOffset)
        pictureBox_GreenArrow.Location = new Point((xCooNew + intervalOffset), yCoo);

    }

Once the PictureBox reaches it limit (interval_value + intervalOffset_value) the PictureBox start from the beginning again!

The vars:

  • interval
  • intervalOffset

Can be modified to use it for your application!

Regards, Ricardo

Upvotes: 0

Johnny
Johnny

Reputation: 195

For special condition like using a Radiobutton, proceed as followed:

 using System.Drawing;

 private void rbtPruef_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtDePruef.Checked)
        {
            pictureBox2.Location = new Point(112, 80);
            pictureBox1.Location = new Point(242, 80);
        }
        else
        {
            pictureBox2.Location = new Point(242, 80);
            pictureBox1.Location = new Point(112, 80);
        }
    }

Upvotes: 3

Oliver
Oliver

Reputation: 45071

You can change the ordering by setting the index of the child control (pictureBox) within the container control (flowLayoutPanel):

var index1 = flowLayoutPanel1.Controls.IndexOf(pictureBox1);
var index2 = flowLayoutPanel1.Controls.IndexOf(pictureBox2);

flowLayoutPanel1.Controls.SetChildIndex(pictureBox1, index2);
flowLayoutPanel1.Controls.SetChildIndex(pictureBox2, index1);

Upvotes: 8

Nogard
Nogard

Reputation: 1789

If you want to swap their positions just swap their Location properties:

int tmp_X = p1.Location.X;
int tmp_Y = p1.Location.Y;

p1.Location.X = p2.Location.X;
p1.Location.Y = p2.Location.Y;

p2.Location.X = tmp_X;
p2.Location.Y = tmp_Y;

To force visual relocation invoke following command:

p1.Invalidate();
p2.Invalidate();

Upvotes: 0

happygilmore
happygilmore

Reputation: 3095

You can create a function

public void SwapLocations(ref Point p1, ref Point p2)
{
    Point temp = p1;
    p1 = p2;
    p2 = temp;
}

then call it

SwapLocations(pictureBox1.Location, pictureBox2.Location);

Upvotes: 1

Toon Casteele
Toon Casteele

Reputation: 2579

Point loc1 = pictureBox1.Location;
Point loc2 = pictureBox2.Location;

pictureBox1.Location = loc2;
pictureBox2.Location = loc1;

EDIT: With one point assignment:

Point temp = pictureBox1.Location;

pictureBox1.Location = pictureBox2.Location;
pictureBox2.Location = temp;

Upvotes: 0

Related Questions