Question

I have a form which initialy sizes itself to its contents (a UserControl placed on location 0, 0) by setting AutoSize to true and AutoSizeMode to GrowAndShrink.

After initialization, I want the form to take control and turn off AutoSizing and make the UserControl Dock.Fill.

The problem is that when turning off AutoSize, the form regains its previous size instead of keeping its current size. I can store the Size before turning off AutoSize, and restore it afterwards, but you still get sizing events (you could see the form flicker).

Is there any way to have a form keep its current size and not have any sizing events fired when turning off AutoSize?

Était-ce utile?

La solution

Try setting the MinimumSize property of the form before turning off the AutoSize property:

this.MinimumSize = this.Size;
this.AutoSize = false;

Autres conseils

I had a similar problem - I wanted the form to grow or shrink using AutoSize when a button was pressed, and then return to allowing the user to resize it manually.

Currently my solution is in the button event:

    Me.AutoSize = True
    uiStopAutoSizeTimer.Start()

And then have a separate method to both turn off the AutoSize, and keep the size that AutoSize had used on the form.

Private Sub uiStopAutoSizeTimer_Tick(sender As System.Object, e As System.EventArgs) Handles uiStopAutoSizeTimer.Tick
    Me.SuspendLayout()
    Dim tempSize = Me.Size
    Me.AutoSize = False
    Me.Size = tempSize
    uiStopAutoSizeTimer.Enabled = False
    Me.ResumeLayout()
End Sub

It works, but it just feels clunky.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top