【问题标题】:Child control re-positioned on a auto scroll form when the control is shown显示控件时,子控件重新定位在自动滚动表单上
【发布时间】:2012-10-31 14:21:21
【问题描述】:

我有一个将AutoScroll 设置为true 的表单。窗体上的某些控件在窗体启动时是不可见的,但它们的位置已经设​​置。一旦控件变为可见,就会计算相对于当前AutoScrollPosition 的位置,这是完全不需要的。

知道如何防止这种情况吗?

我尝试设置控件首次显示时的位置,如下:

 void OnControlVisibleChanged(object sender, EventArgs e)
    {
        Control ctl = (Control)sender;
        if (ctl.Visible)
        {
            ctl.Location = Point.Add(ctl.Location, new Size(this.AutoScrollPosition));
            ctl.VisibleChanged -= OnControlVisibleChanged;
        }
    }

它对某些控件有效,而对其他控件无效。我不知道为什么。
编辑:似乎当控件变为可见时,真正的 AutoScrollPosition 会发生变化,但 AutoScrollPosition 属性不会立即更新。

【问题讨论】:

  • 具有AutoSize = True 的控件似乎不受此影响。它们无需任何修改即可正确放置在面板中。
  • @LarsTech 感谢您的努力。我很想听听你的结论。

标签: .net winforms autoscroll


【解决方案1】:

AutoSize = True 似乎干扰了您的代码。我不知道这是否是唯一的失败点,但这在我的测试中有效:

void OnControlVisibleChanged(object sender, EventArgs e) {
  Control ctl = (Control)sender;
  if (ctl.Visible) {
    bool moveOK = true;
    if (ctl.GetType().GetProperty("AutoSize") != null) {
      if ((bool)ctl.GetType().GetProperty("AutoSize").GetValue(ctl, null)) {
        moveOK = false;
      }
    }
    if (moveOK) {
      ctl.Location = Point.Add(ctl.Location, new Size(this.AutoScrollPosition));
    }
  }
}

【讨论】:

  • 哇,这正是我的情况!现在一切都很完美。我永远不会想到这一点。非常感谢您,先生。
猜你喜欢
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多