因此,我有一个类'employee
',其中具有属性double X
和double Y
,并且我想将这两个属性绑定到表单的位置。 (左上角)我尝试使用How to get the position of a Windows Form on the screen?
但这只会给出无用的值。
如何访问实际属性?
到目前为止已经尝试过:
this.Left.DataBindings.Add("Value", EmpNd, "ThisEmployee.X", true, DataSourceUpdateMode.OnValidation);
答案 0 :(得分:1)
假设您有两个文本框,它们将显示表单的X
和Y
坐标。在表单的Load
事件中,您可以像这样绑定表单的DesktopLocation.X
和DesktopLocation.Y
属性:
private void Form1_Load(object sender, EventArgs e)
{
txtX.DataBindings.Add("Text", this.DesktopLocation.X, null);
txtY.DataBindings.Add("Text", this.DesktopLocation.Y, null);
}
如果希望文本框在移动表单时显示更新的值,则可以声明一个执行此操作的方法,并在发生Form_Move()
事件时调用它:
private void Form1_Move(object sender, EventArgs e)
{
RefreshDataBindings();
}
public void RefreshDataBindings()
{
txtX.DataBindings.Clear();
txtY.DataBindings.Clear();
txtX.DataBindings.Add("Text", this.DesktopLocation.X, null);
txtY.DataBindings.Add("Text", this.DesktopLocation.Y, null);
}