How can I implement the AutoSize property for a user control?

I want to implement the AutoSize property in a user control (and not in a user control) in such a way that it behaves like other standard .NET WinForms controls that implement AutoSize (ala CheckBox) in design mode.

I have a property, but this is how the behavior in design mode controls that scares me. it can still be changed, which makes no sense, because the visual size is not reflected in the AutoSize and Size properties that I implemented.

Standard .NET controls do not allow resizing (or even showing resizing handles) in design mode when AutoSize is true. I want my control to behave the same.

Change I work using the override of SetBoundsCore (), but it does not visually limit the size; if AutoSize is set to true, it has the same effect; functionality is equivalent, but it seems unnatural.

protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) { if (!this.auto_size) this.size = new Size(width, height); base.SetBoundsCore(x, y, this.size.Width, this.size.Height, specified); } 

Any ideas on this standard?

+4
source share
4 answers

Here is my recipe for automatic startup control.

Create the GetAutoSize () method to calculate the required size of the control according to the specific implementation. Perhaps this is the size of the text that it contains, or the total height of the controls for the current width, whatever.

Create a ResizeForAutoSize () method to force the control to resize after changing its state. For example, if the control has a size for the text that it contains, the change in text should have a change in the size of the control. Just call this method when the text changes.

Override GetPreferredSize () to notify anyone who wants to know (e.g. FlowLayoutPanel) what size we have.

Override SetBoundsCore () to enforce our calibration rule in the same way that you cannot resize an AutoSize label.

See an example here.

 /// <summary> /// Method that forces the control to resize itself when in AutoSize following /// a change in its state that affect the size. /// </summary> private void ResizeForAutoSize() { if( this.AutoSize ) this.SetBoundsCore( this.Left, this.Top, this.Width, this.Height, BoundsSpecified.Size ); } /// <summary> /// Calculate the required size of the control if in AutoSize. /// </summary> /// <returns>Size.</returns> private Size GetAutoSize() { // Do your specific calculation here... Size size = new Size( 100, 20 ); return size; } /// <summary> /// Retrieves the size of a rectangular area into which /// a control can be fitted. /// </summary> public override Size GetPreferredSize( Size proposedSize ) { return GetAutoSize(); } /// <summary> /// Performs the work of setting the specified bounds of this control. /// </summary> protected override void SetBoundsCore( int x, int y, int width, int height, BoundsSpecified specified ) { // Only when the size is affected... if( this.AutoSize && ( specified & BoundsSpecified.Size ) != 0 ) { Size size = GetAutoSize(); width = size.Width; height = size.Height; } base.SetBoundsCore( x, y, width, height, specified ); } 
+4
source

In the constructor of your control, call SetAutoSizeMode (AutoSizeMode.GrowAndShrink).

0
source

Override the SizeFromClientSize() method. in this method, you must calculate the required size for your control and return it.

0
source

You only need to add these two lines on top of your custom control declaration

 [Designer("System.Windows.Forms.Design.LabelDesigner")] [ToolboxItem("System.Windows.Forms.Design.AutoSizeToolboxItem")] 

And itโ€™s natural to implement your Autosize logic

0
source

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


All Articles