C# Winform FlowLayoutPanel 子控件重叠问题调查处理

.Net技术 码拜 5年前 (2019-11-27) 5014次浏览 0个评论

最近用C# Winform 做个图片浏览软件,类似Windows的资源管理器,缩略图查看图片。用户反馈,最下方的图片出现重叠显示。软件中用了FlowLayoutPanel来自适应布局,按理不应该出现重叠。如图:
C# Winform FlowLayoutPanel 子控件重叠问题调查处理
网上找了半天没有找到原因,试了其他控件不显示图片,仍然可以重现重叠的问题。
最后找到的原因是 FlowLayoutPanel 的显示高度超过了限制。最大高度是 32984
解决方法:
加入的控件累积的高度不能超过 32984 ,减少控件数量或者减小控件高度。

FlowLayoutPanel Reached the max height.

I just finished running some tests. I was able to Top Dock 152 panels each having a height of 217. On the 153rd panel I saw a perfect eclipse of the 152nd panel. This gives us a maximum height of 32,984 when using a top docking strategy in a scrolling enabled panel.

At this point I would suggest using paging in your record display, since you know how tall each control is which will display a record, you can set the maximum page size to ensure that a user cannot select so many records that one display eclipses the other. The user will then be able to page through records by as many or few as they would like (within limits) without seeing even a partial eclipse.

An extension method for object paging in memory might look something like this…

/// <summary>Provides a paging mechanisim over an IEnumerable interface</summary>
/// <typeparam name="TSource">The type of object</typeparam>
/// <param name="source">The IEnumerable object</param>
/// <param name="start">the zero based start record</param>
/// <param name="pageSize">the page size</param>
/// <returns>A page size (or less if not available) of data</returns>
public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int start, int pageSize)
{
    return source.Skip(start).Take(pageSize);
}

For a static helper method instead of an extension method, just remove the word “this” from the method signature.

 

From the picture, the main issue is that the last RichTextBox in the FlowLayoutPanel cannot be displayed completely when the height of the FlowLayoutPanel increases to a big value. Based on my understanding, there are two ways to workaround this:

1. Set the height of the FlowLayoutPanel to a certain value. We do not need to find the maximum value, just set a value big enough and the controls added to it can be layouted well. Then we can use paging technic to show all the controls. For example, you can add a button named PreviousButton, a button named NextButton. The handles of the two buttons will go the previous or next page. When the page index is changed, the controls in the FlowLayoutPanel are just removed and new controls are added. A better way is to keep the controls unchanged and update their states, such as setting Text property.

2. Use Panel to contain the controls and handle its Layout event to move the control to its correct place manually. This way is a little complicated because we need to calculate the positions of the controls, but it is more flexible and its performance is better.

Let me know if this does not help.
Aland Li

参考:

https://social.msdn.microsoft.com/Forums/windows/en-US/8e8f0e7b-332e-4d85-b64b-dea11ce16a2c/reached-the-max-height?forum=winforms

https://stackoverflow.com/questions/7647244/flowlayoutpanel-height-bug-when-using-autosize


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# Winform FlowLayoutPanel 子控件重叠问题调查处理
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!