更改PictureBox的位置

时间:2013-02-27 13:01:49

标签: c# winforms visual-studio

我在flowLayout面板上有2个pictureBox,需要将pictureBox1移动到pictureBox2的位置,将pictureBox2移动到pictureBox1的位置

编辑:

我已经尝试了但是boxBoxes没有移动...我使用MessageBox来检查位置......位置是正确的但他们不交换位置(位置没有改变......)

     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);

6 个答案:

答案 0 :(得分:8)

您可以通过在容器控件(pictureBox)中设置子控件(flowLayoutPanel)的索引来更改排序:

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

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

答案 1 :(得分:3)

对于使用单选按钮等特殊情况,请按以下步骤操作:

 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);
        }
    }

答案 2 :(得分:1)

您可以创建一个功能

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

然后调用它

SwapLocations(pictureBox1.Location, pictureBox2.Location);

答案 3 :(得分:0)

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

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

编辑: 有一个点分配:

Point temp = pictureBox1.Location;

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

答案 4 :(得分:0)

如果您想交换他们的头寸,只需交换他们的Location属性:

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;

强制视觉重定位调用以下命令:

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

答案 5 :(得分:0)

由于我在某个间隔移动PictureBox ,我也有非常大问题,我决定编写一个有用的功能。该函数每次调用Timer时都会被调用。该函数的 Sense 从(0 +偏移)开始移动图片框直到(end_point_of_movement + Offset) 。也许这个功能对你们中的一些人很有用:

    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);

    }

一旦PictureBox 到达限制(interval_value + intervalOffset_value),PictureBox 再次从头开始!

vars:

  • 间隔
  • intervalOffset

可以修改以将其用于您的应用程序!

此致 里卡多