Search code examples
c#.netwinformsautosize

What is the best practice for implementing AutoSize on custom controls?


What is the best practice to implement autosizing on custom Windows Forms controls? For instance, a custom Label control inheriting from the System.Windows.Forms.Label class.

The Control.ClientSize article on MSDN has an example on how to set the new size (using the ClientSize property), but it doesn't give a hint on what events this method should be called: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.clientsize.aspx

I tried different methods without success (ex: overriding SizeFromClientSize, OnTextChanged, etc.). It seems that as soon as the AutoSize property is true, I cannot override the ClientSize value successfully.

Can anyone help? Thanks!


Solution

  • Better derive your control directly from Control, which has no AutoSize property and no related logic which interferes with your own logic.