【问题标题】:How to access form's location propertie?如何访问表单位置属性?
【发布时间】:2019-09-27 17:38:03
【问题描述】:

所以我有类'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);

【问题讨论】:

  • 请先展示你的作品。
  • @Sach 我应该显示什么?我有一个表单和一个类,在类中,我想使用 DataBindings.Add 方法将 x 和 y 属性绑定到表单。
  • 所以你的代码。展示您迄今为止尝试过的内容。
  • @Sach 添加了你想要的内容
  • 您应该按照此处的建议创建一个 MCVE:stackoverflow.com/help/minimal-reproducible-example

标签: c# forms winforms data-binding properties


【解决方案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);
}

【讨论】:

  • 是的,但是如何将它与类属性绑定?
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
相关资源
最近更新 更多