现在有个问题想请教一下各位:
现在有两个窗体Form1和Form2,Form1中有一个Label,Form2中有四个按钮分别代表上下左右,怎么用Form2中的四个按钮控制Form1中的Label的位置呢?比如,我在Form2中按上,Form1中的Label就上移一个像素。另外如何控制Form1窗体的位置和大小呢?
如下图所示。C#
现在有两个窗体Form1和Form2,Form1中有一个Label,Form2中有四个按钮分别代表上下左右,怎么用Form2中的四个按钮控制Form1中的Label的位置呢?比如,我在Form2中按上,Form1中的Label就上移一个像素。另外如何控制Form1窗体的位置和大小呢?
如下图所示。C#
解决方案 »
- 【求高手指点】C#中protected用法和private用法怎么有些地方这么相同??
- 客户端通过xmlhttp.send(str)传给服务器端的str参数要怎么获取。 不要通过域名网址传参,和send内部变量传参。 就单独的获取一个没有变量的参数
- 如何对comboBox绑定DataTable对象?
- 找一个通用函数,修改同一类别的控件的一些属性,或者优化、重构下面的代码。
- 如何返回一个日期所在星期的所有日期
- 用C#写货物进出口记录的程序。
- 关于listview中的checkbox难题
- 请问哪里有下载 Reporting services (测试版的也可)谢谢!!!!!
- 请教各位高手一个问题。。。。。
- DataGrid如何响应右键?
- 使用Newtonsoft.Json.JsonConvert但是发现基类中的字段都没有转?
- 在windows mobile 使用Newtonsoft.Json 报错?
让Label上移一个像素,直接改坐标Y值就可以实现呀
public OffsetLabel(int x, int y)
{
this.label.Location = new Point(this.label.Location.X + x, this.label.Location.Y + y);
}
在Form2中form1.OffsetLabel(-1, 0);//左移一个像素
form1.OffsetLabel(1, 0);//右移一个像素
form1.OffsetLabel(0, -1);//上移一个像素
form1.OffsetLabel(0, 1);//下移一个像素
控制form1的位置form1.Location = new Point(...);
{
public Form1()
{
InitializeComponent(); Form2 form = new Form2();
form.Show(); this.KeyDown += delegate(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
form.btn.Top -= 5;
break;
case Keys.Down:
form.btn.Top += 5;
break;
case Keys.Left:
form.btn.Left -= 5;
break;
case Keys.Right:
form.btn.Left += 5;
break;
default:
break;
}
}; this.Paint += delegate(object sender, PaintEventArgs e)
{
e.Graphics.DrawString("按上下左右移动Form2窗体里面的Btn控件", new Font("宋体", 10), Brushes.Black, new Point(20, 20));
};
}
} class Form2 : Form
{
public Button btn; public Form2()
{
this.Controls.Add(btn = new Button());
}
}
不谢
这个是在Form1中new的Form2,如果这个两个窗体都是其父窗体建立的呢?(两个不同的button分别new 了一个窗口)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
Form2 frm;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
frm = new Form2();
frm.parentFrm = this;
frm.Show();
} public void resetPos()
{
this.button1.Location = new Point(frm.xIndex,frm.yIndex); }
}
}Form2:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{ public partial class Form2 : Form
{
public int xIndex =0;
public int yIndex=0;
public Form1 parentFrm;
public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.xIndex -= 1;
parentFrm.resetPos();
} private void button2_Click(object sender, EventArgs e)
{
this.yIndex -= 1;
parentFrm.resetPos();
} private void button3_Click(object sender, EventArgs e)
{
this.xIndex += 1;
parentFrm.resetPos();
} private void button4_Click(object sender, EventArgs e)
{
this.yIndex += 1;
parentFrm.resetPos();
}
}
}
public static int intTop = 0;
public static int intLeft= 0;类名.intTop 就可以访问控制Form2 开个定时器刷新位置
Form1 点下控制位置
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); Form2 form2 = new Form2();
form2.Show();
Form3 form3 = new Form3();
form3.from = form2;
form3.Show();
this.Text = "我是父窗体,是我分别了new 了 Form2 和Form3出来";
}
} class Form2 : Form
{
public Button btn; public Form2()
{
this.Text = "我是form2";
this.Controls.Add(btn = new Button());
}
} class Form3 : Form
{
public Form2 from { get; set; } public Form3()
{
this.Text = "我是form3";
this.KeyDown += delegate(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
from.btn.Top -= 5;
break;
case Keys.Down:
from.btn.Top += 5;
break;
case Keys.Left:
from.btn.Left -= 5;
break;
case Keys.Right:
from.btn.Left += 5;
break;
default:
break;
}
}; this.Paint += delegate(object sender, PaintEventArgs e)
{
e.Graphics.DrawString("按上下左右移动Form2窗体里面的Btn控件", new Font("宋体", 10), Brushes.Black, new Point(20, 20));
};
}
}
把Form2 f2 放在外面去定义不就行了
至于说你的 btnOpen2_Click 中访问 f2 的问题,实际上把 f2变量定义为 private Form2 f2; 就行了,因为你的主窗口是这两个子窗体的宿主,它知道两个窗体分别是否new过了(有痕迹能够记录)。这不是设计问题,只是一个声明变量的问题。
{
Form2 f;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler( Form1_Load );
} void Form1_Load( object sender , EventArgs e )
{
f = new Form2();
f.MovingHandler += new OnMovingCommandHandler( f_MovingHandler );
f.Show();
} void f_MovingHandler( int flag )
{
//移动控件
switch ( flag )
{
case 1:
button1.Left++;
break;
case 2:
button1.Left--;
break;
case 3:
button1.Top++;
break;
case 4:
button1.Top--;
break;
}
}
}=================以下是四个按钮的控制窗口public delegate void OnMovingCommandHandler( int flag);public partial class Form2 : Form
{
public event OnMovingCommandHandler MovingHandler; public Form2()
{
InitializeComponent(); this.button1.Click += new EventHandler( button1_Click );
this.button2.Click += new EventHandler( button1_Click );
this.button3.Click += new EventHandler( button1_Click );
this.button4.Click += new EventHandler( button1_Click );
} void button1_Click( object sender , EventArgs e )
{
Button bt = (Button)sender; if ( MovingHandler != null )
{
int flag = 0;
if ( bt.Name == button1.Name )
{
flag = 1;
}
if ( bt.Name == button2.Name )
{
flag = 2;
}
if ( bt.Name == button3.Name )
{
flag = 3;
}
if ( bt.Name == button4.Name )
{
flag = 4;
}
MovingHandler( flag );
}
}
}