关于 TableLayoutPanel 的 Scroll bar

为简化多个控件的布局,我在项目中使用了 TableLayoutPanel,并且设置了 AutoScroll = true,以实现滚动条功能。QA 在测试过程中发现两个问题:

  1. TableLayoutPanel 内容过多时,达到某一个高度后,其下的所有行都不能正确显示。
  2. 使用鼠标移动滚动条,再切换到另一个窗体或程序,重新激活该窗体后滚动条又恢复到最初状态,页面保持在最上方。

通过了多次 Google,基本搞清楚了是什么原因:

  1. 这是 TableLayoutPanel 的一个 bug,经过测试,FlowLayoutPanel 也存在同样的问题。一个老外也遇到了同样的问题,并提供了解决方法:

    While I didn't figure out why FlowLayoutPanel doesn't draw the items below a certain point, I found a workaround.
    Attach an event handler to Scroll, where you perform FlowLayoutPanel.PerformLayout() and it will draw the controls when you get to them.

  2. 原以为也是 Microsoft 的 Bug,可是看了官方的回复,我也只有 workaround 了。
    Due to the fact that it is the previously shipped behavior of Form to always scroll the active control into view when the Form is activated, we will not be able to change this. We can consider a property to disable this functionality, but this feature will not make the Whidbey release. We will consider it for a future release.

下面是解决该上面问题的代码,供参考:

 

private void WFPanel_Load(object sender, EventArgs e)
{
    flpCharts.MouseWheel
+= flpCharts_MouseWheel;
}

private void flpCharts_MouseWheel(object sender, MouseEventArgs e)
{
    flpCharts.Focus();
}

private void flpCharts_Scroll(object
sender, ScrollEventArgs e)
{
    flpCharts.PerformLayout();
    flpCharts.Focus();
}

 

其中,Scroll 事件的第一和第二行代码,分别是为了解决前面提到的两个问题。而 MouseWheel 事件,则是为了处理第二个问题使用鼠标滚轮的场合。因为 MouseWheel 事件在 Designer 中找不到,只有在 Load 事件中手动注册了。

你可能感兴趣的:(关于 TableLayoutPanel 的 Scroll bar)