Winforms:具有数千个用户控件的可滚动 FlowLayoutPanel - 如何防止内存泄漏并以正确的方式处理对象? [英] Winforms: Scrollable FlowLayoutPanel with thousands of user controls - How to prevent memory leaks and dispose objects the right way?

查看:86
本文介绍了Winforms:具有数千个用户控件的可滚动 FlowLayoutPanel - 如何防止内存泄漏并以正确的方式处理对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 windows 窗体编程比较陌生.我正在开发一个 windows 窗体应用程序,它需要用户滚动窗体(我在窗体中使用了一个 FlowLayoutPanel,其中 AutoScroll=True).Panel 的源代码包含一个用户控件,该控件根据记录数循环.

I am relatively new to windows form programming. I am developing a windows form application which requires user to scroll through the form (I have used a FlowLayoutPanel with AutoScroll=True inside the form). The Panel's source contains an user control which is looped through depending on the number of records.

例如,如果 DB 中存在 10 条记录,那么在加载表单时会创建 10 个用户控件并将其添加到面板中,并且用户将能够滚动浏览这 10 个项目.(请参阅描述的示例图像这个场景)示例屏幕图像

For instance, if 10 records are present in DB, then 10 user controls are created and added into the Panel when the Form loads, and user will be able to scroll through the 10 items.(Please see the sample image which depicts this scenario) Sample Screen Image

问题:

当有更多记录(比如 1000)时会出现问题,因为没有处理用户控件对象并且超出了句柄限制,应用程序崩溃(带有消息:错误创建窗口句柄).我知道这是内存泄漏或设计不当的问题.

The problem occurs when there are more number of records (say 1000), since the usercontrol objects are not disposed and the Handle limit is exceeded, the application crashes (with a message: Error creating window handle). I am aware that this is a memory leak or issue with a bad design.

但我无法在这里找到可靠的解决方案,我已经考虑了以下选项(以克服该问题)但不确定如何继续:

But I am unable to find a reliable solution here, i have thought about the below options (to overcome the issue) but not sure how to proceed on these:

  1. 仅加载用户屏幕上可见的 UserControls 并动态处理其他 userControls(当用户滚动或按下向上/向下按钮时)
  2. 是否可以在面板中将用户控件呈现为图像,并且在悬停或单击任何部分时,可以重新初始化用户控件并触发加载(这样,实际的用户控件对象将不会是在内存中)

您能否建议一个合适的解决方案/在此要求的上下文中处理 UserControl 对象的正确方法

Could you please suggest a suitable solution / the right way of handling the UserControl objects in the context of this requirement

推荐答案

我建议使用 DataGridView.

我将能够保存 1000 条记录.

I will be able to hold 1000s of records.

您可以选择其中之一

  • 坚持其直网格状布局或
  • 选择混合布局

我的意思是一种解决方案,它将除当前记录之外的所有记录显示为网格中的一行;当前的一个可以被你的 UserControl 的一个实例放大和覆盖,只要选定的行发生变化,它就会从底层行的数据中加载.

By that I mean a solutuion that shows all but the current record as one row in the grid; the current one can be enlarged and overlaid by one instance of your UserControl that gets loaded from the underlying row's data whenever the selected row changes.

请参阅这篇文章这种技术的例子;在那里我展示了如何用更大的区域替换当前行,这与 手风琴 控件的工作方式非常相似..

See this post for an example of this technique; there I show how to replace the current row with a larger area, much in the way an accordion control would work..

DGV 中的行实际上只是一个 Bitmap,因此滚动起来很便宜;使用 DGV 的正常方法是依靠其使用正确类型的编辑控件覆盖一个单元格的能力.

The rows in the DGV are in fact just a Bitmap and therefore cheap to scroll; the normal way to use a DGV is to rely on its ability the overlay one cell with an edit control of the right type.

手风琴技巧"将其扩展到覆盖整行的任意UserControl.

The 'accordion trick' extends this to an arbitrary UserControl overlaying a whole row.

请注意,其他行将占用更少的空间,我认为这是一个奖励..

Note that the other rows will take a lot less space, which I see as a bonus..

这篇关于Winforms:具有数千个用户控件的可滚动 FlowLayoutPanel - 如何防止内存泄漏并以正确的方式处理对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆