改变PictureBox的位置

3

我在一个流式布局面板上有两个pictureBox,需要将pictureBox1移动到pictureBox2的位置,并将pictureBox2移动到pictureBox1的位置。

编辑:

我已经尝试了,但是pictureBoxes没有移动...我使用了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);

是的...调换它们之间的位置...明白我的意思吗? - Ladessa
@Nogard 看起来 OP 想要交换图片框。 - David Goshadze
你能交换这些内容吗? - Sayse
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);

1
我本来也要发布完全相同的代码。只是变量名不同 :) - David Goshadze
尝试使用标签,但交换位置没有起作用。这个方法可以。 - Corak

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

1
你可以创建一个函数。
public void SwapLocations(ref Point p1, ref Point p2)
{
    Point temp = p1;
    p1 = p2;
    p2 = temp;
}

然后调用它。
SwapLocations(pictureBox1.Location, pictureBox2.Location);

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;

你只需要定义一个点,然后将pb1设置为temp变量,将pb2设置为pb1,然后将temp设置为pb1。 - Sayse
我已经尝试过了,但是pictureBoxes不会移动...它们仍然在同一个位置。 - Ladessa
@Sayse 我知道,这只是为了让它更清晰易懂。我会编辑你的评论。 - Toon Casteele
如果这是您尝试过的代码,则应将其作为对原始问题的编辑发布。 - Toon Casteele
哦,你说得对,我需要更好地阅读问题 :). 那么我想知道为什么您需要将pictureBoxes放在flowLayoutPanel中? - Toon Casteele
显示剩余2条评论

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

0
由于我在特定时间间隔内移动PictureBox方面遇到了非常大的问题,因此我决定编写一个有用的函数。该函数会在计时器的每个Tick上调用。该函数的意义是从(0 +偏移量)开始移动picturebox直到(移动结束点+偏移量)。也许这个函数对你们中的一些人有用:
    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将重新从开头开始!
变量:
- interval - intervalOffset
可以修改以用于您的应用程序!
问候, Ricardo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接