Windows Forms Application Performance

My application has many controls on its surface, and it is added dynamically at runtime.

Although I use tabs to limit the number of controls displayed and double buffering, it still flickers and stutters when it has to redraw (resize, maximize, etc.).

What are your tips and tricks for improving WinForms application performance?

+4
source share
4 answers

I know two things you can do, but they do not always apply to all situations.

  • You will get better performance if you use absolute positioning for each control (myNewlyCreatedButton.Location.X / Y) as opposed to using the flow layout panel or the table layout panel. WinForms has to do a lot less math, trying to figure out where the controls should be placed.

  • If there is one operation in which you add / remove / modify many controls, call "SuspendLayout ()" in the container of the affected controls (either the panel or the entire form), and when you are done, call "ResumeLayout ()" on the same panel. If you do not, the form will have to lay out the layout each time you add / remove / modify a control that costs a lot more time. see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout(VS.80).aspx

Although, I'm not sure how these approaches can be applied when resizing a window.

+4
source

Although more general than some other tips, here is mine:

When using a large number of "elements", try to avoid creating a control for each of them, and not to reuse the controls. For example, if you have 10,000 elements, each of which corresponds to a button, it is very simple (programmatically) to create 10,000 buttons and connect their event handlers so that when you enter the event handler you know exactly which element you should work with. However, this is much more efficient if you create, say, 500 buttons (because you know that only 500 buttons will be displayed on the screen) and enter a โ€œdisplay layerโ€ between the buttons and the elements that dynamically reassign the buttons to different elements every time the user does something that will change the set of buttons that should be visible (for example, moving the scroll bar).

+3
source

Although, I'm not sure how these approaches can be applied when resizing a window.

Handle the ResizeBegin and ResizeEnd events to call SuspendLayout () and ResumeLayout (). These events apply only to the System.Windows.Form class (although I would like them to also be in Control).

+2
source

Source: https://habr.com/ru/post/1276550/


All Articles