3

我在 flowLayout 面板上有 2 个图片框,需要将图片框 1 移动到图片框 2 的位置,将图片框 2 移动到图片框 1 的位置

编辑:

我已经尝试过,但图片框不移动...我使用 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);
4

6 回答 6

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);
于 2013-02-27T13:13:49.300 回答
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);
        }
    }
于 2013-12-06T13:44:55.913 回答
1

你可以创建一个函数

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

然后调用它

SwapLocations(pictureBox1.Location, pictureBox2.Location);
于 2013-02-27T13:11:12.247 回答
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;
于 2013-02-27T13:05:52.903 回答
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();
于 2013-02-27T13:12:01.540 回答
0

由于我在某个区间内移动 PictureBox时也遇到了很大的问题,因此我决定编写一个有用的函数。该函数在计时器的每个滴答声中被调用。该函数的意义将图片框从 (0 + Offset) 开始移动到 (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又从头开始!

变量:

  • 间隔
  • 间隔偏移

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

问候,里卡多

于 2017-03-23T11:32:16.110 回答