我互换以后总是他自己就变回去了 是怎么回事。
int a = pictureBox1.Location.X;
int b = pictureBox1.Location.Y;
int a1 = pictureBox2.Location.X;
int b1 = pictureBox2.Location.Y;
if (textBox1.Text == "1" && textBox2.Text == "2")
{
pictureBox1.Location = new System.Drawing.Point(a1, b1);
pictureBox2.Location = new System.Drawing.Point(a, b);
}
private void button4_Click(object sender, EventArgs e)
{
int a = pictureBox1.Location.X;
int b = pictureBox1.Location.Y;
int a1 = pictureBox2.Location.X;
int b1 = pictureBox2.Location.Y;
int a2 = pictureBox3.Location.X;
int b2 = pictureBox3.Location.Y;
if (textBox1.Text == "1" && textBox2.Text == "2")
{
pictureBox1.Location = new System.Drawing.Point(a1, b1);
pictureBox2.Location = new System.Drawing.Point(a, b);
}
if (textBox1.Text == "1" && textBox2.Text == "3")
{
pictureBox1.Location = new System.Drawing.Point(a2, b2);
pictureBox3.Location = new System.Drawing.Point(a, b);
}
C#控件位置的互换
答案:3 悬赏:80 手机版
解决时间 2021-03-14 16:04
- 提问者网友:别再叽里呱啦
- 2021-03-13 23:12
最佳答案
- 五星知识达人网友:掌灯师
- 2021-03-14 00:18
你的picturebox是前台添加的还是后台自动生成的? 如果是前台添加的 你的这段代码在页面提交后就没用了。。。
全部回答
- 1楼网友:duile
- 2021-03-14 03:00
估计你又重新triger 了一次event, 于是他们就换回去了...
把代码再贴完整点
- 2楼网友:鱼忧
- 2021-03-14 01:40
我以前做过一个贪吃蛇,蛇不能碰到障碍物,我想和你需要的情况类似
要用到控件的几个位置属性,location(顶点坐标),width,heigth,left(location.x),right(location.x+width),top(location.y),bottom(location.y+heigth)
先拖一个panel1
加个方法
private bool istouch(control c)
{
foreach (control con in this.panel1.controls)
{//如果在这个范围内,表示此控件会存在覆盖
if (c.right >= con.left && c.left <= con.right && c.bottom >= con.top && c.top <= con.bottom)
return true;
}
return false;
}
如下是测试代码:
button b = new button();
b.location = new point(20,50);//左上角顶点坐标
b.width = 50;//宽
b.height = 50;//高
b.text = "test";
if (!istouch(b))//如果不会覆盖,添加到panel
{
this.panel1.controls.add(b);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯