问题描述
对于两种方法 Control.SuspendLayout
和 BeginUpdate $ c $之间的根本区别到底是什么,我找不到很好的解释。 c>(通常在列表控件中看到,例如
ListView
, ComboBox
, ListBox
等),除了它们都可以提高性能。
I do not find a good explanation on what actually the underlying difference is between the two methods Control.SuspendLayout
and BeginUpdate
(typically seen on list controls like ListView
, ComboBox
, ListBox
etc), other than that they both improve performance.
据我了解:
-
它们都暂停绘图直到加载要显示的所有项目,然后再重新绘制。
they both suspend drawing until all the items to display are loaded, and repaint after that.
通常<$ c $将控件添加到容器控件(如 Panel
, GroupBox
等容器控件时,将调用c> SuspendLayout ,而 BeginUpdate
用于将非控件项(如对象)添加到列表控件(如 ListBox
)中。
typically SuspendLayout
is called when controls are added to container controls like Panel
, GroupBox
etc, while BeginUpdate
is used for adding non-control items like objects to list controls like ListBox
.
但是为什么在执行相同的操作时会有两个呼叫?还是它们有什么不同?
But why are there two calls when they do the same? Or what do they do differently?
类似地,还有 ResumeLayout
和 EndUpdate
等效项。
Similarly there is ResumeLayout
and EndUpdate
equivalents.
推荐答案
它们没有共同之处。 SuspendLayout会关闭自动布局,TableLayoutPanel和FlowLayoutPanel等控件使用的自动布局以及从Dock,Anchor和AutoSize属性获取的布局更新。它对ListView,ComboBox或ListBox完全无效,这些控件不执行布局。通常,仅在将控件批量添加到容器中时才使用它。有时,当自动布局使调整窗口大小变得令人讨厌时,您会使用它。它确实减少了重新绘制的次数,完全是因为它暂停了控件的大小更新。
They have nothing in common. SuspendLayout turns off automatic layout, the kind that's used by controls like TableLayoutPanel and FlowLayoutPanel as well as the layout updates you get from the Dock, Anchor and AutoSize properties. It has no effect at all on ListView, ComboBox or ListBox, those controls don't perform layout. You typically only use it when you add controls in bulk to a container. Sometimes you use it when automatic layout makes resizing the window too obnoxious. It does reduce the number of repaints, solely by the fact that it suspends control size updates.
BeginUpdate阻止控件重新绘制自身。当您将项目批量添加到ListView或ListBox之类的控件上时,便会使用它们,而由于某种原因而无法使用它们的Items.AddRange()方法。
BeginUpdate stops a control from repainting itself. You do use it on controls like ListView or ListBox when you add items to them in bulk and can't use their Items.AddRange() method for some reason.
这篇关于SuspendLayout和BeginUpdate之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!