如何访问表格的位置属性?

时间:2019-09-27 17:38:03

标签: c# forms winforms data-binding properties

因此,我有一个类'employee',其中具有属性double Xdouble Y,并且我想将这两个属性绑定到表单的位置。 (左上角)我尝试使用How to get the position of a Windows Form on the screen? 但这只会给出无用的值。

如何访问实际属性?

到目前为止已经尝试过:

this.Left.DataBindings.Add("Value", EmpNd, "ThisEmployee.X", true, DataSourceUpdateMode.OnValidation);

1 个答案:

答案 0 :(得分:1)

假设您有两个文本框,它们将显示表单的XY坐标。在表单的Load事件中,您可以像这样绑定表单的DesktopLocation.XDesktopLocation.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);
}