C# 更改图片框的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15112783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the position of an PictureBox
提问by Ladessa
I have 2 pictureBoxes on a flowLayout Panel and need move the pictureBox1 to pictureBox2's position and pictureBox2 to pictureBox1's position
我在 flowLayout 面板上有 2 个图片框,需要将图片框 1 移动到图片框 2 的位置,将图片框 2 移动到图片框1 的位置
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 来检查位置……位置是对的,但它们不交换位置(位置没有改变……)
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);
采纳答案by Oliver
You can change the ordering by setting the index of the child control (pictureBox
) within the container control (flowLayoutPanel
):
您可以通过pictureBox
在容器控件 ( flowLayoutPanel
) 中设置子控件 ( )的索引来更改排序:
var index1 = flowLayoutPanel1.Controls.IndexOf(pictureBox1);
var index2 = flowLayoutPanel1.Controls.IndexOf(pictureBox2);
flowLayoutPanel1.Controls.SetChildIndex(pictureBox1, index2);
flowLayoutPanel1.Controls.SetChildIndex(pictureBox2, index1);
回答by Toon Casteele
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;
回答by happygilmore
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);
回答by Nogard
If you want to swap their positions just swap their Location
properties:
如果你想交换他们的位置,只需交换他们的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;
To force visual relocation invoke following command:
要强制视觉重定位调用以下命令:
p1.Invalidate();
p2.Invalidate();
回答by Johnny
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);
}
}
回答by Ricardo Fercher
As I had also very big problemswith moving a PictureBoxin a certain IntervalI decided to Code a useful function. The function get called every Tick of the Timer. The Senseof 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:
由于我在某个时间间隔内移动图片框也遇到了很大的问题,因此我决定编写一个有用的函数。该函数在计时器的每个滴答声中被调用。该函数的意义是将一个图片框从 (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);
}
Once the PictureBox reachesit limit(interval_value + intervalOffset_value) the PictureBox start from the beginning again!
一旦图片框达到它的限制(interval_value + intervalOffset_value),图片框又从头开始!
The vars:
变量:
- interval
- intervalOffset
- 间隔
- 间隔偏移
Can be modified to use it for your application!
可以修改以将其用于您的应用程序!
Regards, Ricardo
问候, 里卡多