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

.Net技术 码拜 11个月前 (11-27) 837次浏览 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




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