Reputation: 2543
I made a windows form with many dropdown list which are placed inside a grid(not a datagrid) and when I use anchor=left right top bottom and dock=fill options in all of them, the form resize gets slow on runtime.
What's the problem with dock and anchor options?
Is it a general problem with forms? Is it a general disadvantage of using .net components and windows forms?
I don't have any custom events handled on control resize, so the problem is about the controls Microsoft developed. Should I avoid using dock=fill?
Upvotes: 9
Views: 8121
Reputation: 3512
WinForms drawing performance relies exclusively on CPU processor, single-threaded. This means you will face several performance issues if your form draws pictures, or any other complex/numerous UI elements. So you may want to disable unnecessary redrawing events to optimize the overall performance.
The dirty trick below will give you a huge performance gain by disabling redrawing when resizing occurs in your form. This code below should be placed in your form's class:
protected override void OnResize(EventArgs e)
{
this.SuspendDrawing();
base.OnResize(e);
this.ResumeDrawing();
}
protected override void OnResizeBegin(EventArgs e)
{
this.SuspendDrawing();
base.OnResizeBegin(e);
}
protected override void OnResizeEnd(EventArgs e)
{
base.OnResizeEnd(e);
this.ResumeDrawing();
}
protected override void OnClosing(CancelEventArgs e)
{
this.SuspendDrawing();
base.OnClosing(e);
this.ResumeDrawing();
}
And this is the extension method which does all the trick:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Example
{
public static class ControlExtensions
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
public static void SuspendDrawing(this Control control) => SendMessage(control.Handle, WM_SETREDRAW, false, 0);
public static void ResumeDrawing(this Control control)
{
SendMessage(control.Handle, WM_SETREDRAW, true, 0);
control.Refresh();
}
}
}
Upvotes: 1
Reputation: 62256
It's a normal that it consumes your processor as on every resize move form
forms resize table layout panel
which forces resize and reposition child controls (dropdowns), as there is anchor
property setuped.
There is a trick to avoid to create a frustration for user when resizing:
Use SuspendLayout()
and ResumeLayout()
on BeginResize()
and EndResize()
event handlers.
Look here for concrete example:
Prevent window redraw when resizing c# windows forms
Should work for you.
Upvotes: 20