【问题标题】:Setting child control width in Layout event handler of the FlowLayoutPanel在 FlowLayoutPanel 的 Layout 事件处理程序中设置子控件宽度
【发布时间】:2011-12-07 14:48:34
【问题描述】:

我有一个 flowLayoutPanel,它用子控件动态填充。 此 flowLayoutPanel 可能位于不同表单的某些面板上,因此其大小可能会在运行时发生变化。

添加第一个子控件时,我将其 Width 设置为 flowLayoutPanel.Width - 10。 对于其他控件,我设置 DockStyle = Fill。

还有 flowLayoutPanel_Layout 事件处理程序改变了第一个控件的宽度: flowLayoutPanel.Controls[0].Width = flowLayoutPanel.Width - 10;

在大多数情况下,它可以正常工作,但在其中一个表单上,我遇到了一个问题:加载表单时,flowLayoutPanel 将所有控件的宽度设置为一个值 (127)。当我最大化表单时,使用正确的 flowLayoutPanel.Width(例如,400 像素)调用 flowLayoutPanel_Layout,但在将其设置为 flowLayoutPanel.Width - 10 后 flowLayoutPanel.Controls[0].Width 没有改变。它仍然等于127. 没有任何例外。

什么会导致这种行为?

【问题讨论】:

    标签: c# .net winforms flowlayoutpanel


    【解决方案1】:

    找到问题的根源:这个问题出现在 64 位 Windows Vista 和 7 上,当有很多嵌套控件时: http://blogs.msdn.com/b/alejacma/archive/2008/11/20/controls-won-t-get-resized-once-the-nesting-hierarchy-of-windows-exceeds-a-certain-depth-x64.aspx

    在我的控件中覆盖 OnSizeChanged 有帮助:

    protected override void OnSizeChanged(EventArgs e)
    {
      if (!DesignMode && IsHandleCreated)
        BeginInvoke((MethodInvoker)delegate{base.OnSizeChanged(e);});
      else
        base.OnSizeChanged(e);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      相关资源
      最近更新 更多