vulkanino
vulkanino

Reputation: 9124

WinForms anchored controls don't maximize correctly

I have an issue with WinForms (VB.NET).

The main window is an MDI container. The user opens a new child window:

non maximized child 1

and then maximizes it, so the window correctly fills up the client area. My controls are properly anchored (with the IDE property Anchor) to the window sides so that enlarging the window always fills nicely the client area:

maximized child 1

In this state (client windows maximized) the user opens a different or a new child window, but the new window controls are not stretched, that is they don't "understand" they should stretch!

non stretched child 2

This is particularly annoying because if the user tries to restore the window, then the controls are stretched in, so they disappear (see there's no more the listview)!

restored child 2

Is this a bug? How can I solve this?

edit: as per Hans Passant's comment, I created a new simple project and it worked as it should. So I investigated to see what was different from my real project and the sample. The difference is that in my project I create forms dynamically.

I dynamically create many buttons on a toolbar. When the user clicks a button, this is the code that gets executed:

Private Sub buttonClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Cursor.Current = Cursors.WaitCursor

        Dim b As Button = CType(sender, Button)

        Dim assem As Assembly = Assembly.GetExecutingAssembly()
        Dim formType As Type = assem.GetType(CStr(b.Tag))

        Dim exFormAsObj As Object = Nothing
        Try
            exFormAsObj = Activator.CreateInstance(formType)
        Catch ex As Exception
            Cursor.Current = Cursors.Default
            MessageBox.Show("clicca meglio:" + ex.ToString)
            Exit Sub
        End Try

        Dim f As Form = CType(exFormAsObj, Form)
        f.MdiParent = Me
        f.Show()

        Cursor.Current = Cursors.Default
    End Sub

That is, the form name is in the button tag. I create a new instance of the form, with Activator.CreateInstance(formType) then I show it: f.Show().

I am pretty sure that the problem is in this dynamic child form creation, but I can't get where.

edit2: Found! In my form common Load event I was doing

myform.SuspendLayout()
' various instructions
myform.ResumeLayout(False)

instead of False I should have written true: myform.ResumeLayout(True)

So simple, I'm sorry.

Upvotes: 8

Views: 2683

Answers (4)

JohnOpincar
JohnOpincar

Reputation: 5813

I was using a datagridview on an mdi child form with all four sides anchored and it would size properly as long as the form was created not maximized. If I created the form while other children were maximized, it sized incorrectly and never corrected itself even if you normalized and resized the form manually. My solution was to put a panel on the form with all four sides anchored and placing the DGV in the panel with dock set to file. I have no idea why I had to do this, but it DID fix the problem.

Upvotes: 0

vulkanino
vulkanino

Reputation: 9124

I've found the solution, (thanks Cody Gray to suggesting to post my own answer here).

In my form common Load event I was doing:

myform.SuspendLayout()
'' various instructions
myform.ResumeLayout(False)

instead of False I should have written true: myform.ResumeLayout(True)

Simple, but tricky. Thanks all.

Upvotes: 3

Cocoa
Cocoa

Reputation: 1

My controls are properly anchored (with the IDE property Anchor)

Perhaps try instancing the form and set the properties programmatically in the load event.

If one instance works, but other instances do not, that means you need to inspect the properties of the instanced forms with the debugger to see if they are indeed set the way you expect.

Upvotes: 0

Pedery
Pedery

Reputation: 3636

I think what you might want to achieve is done using

this.LayoutMdi(MdiLayout.TileHorizontal);

or one of its relatives.

Keep in mind that MDI layouts are in general discouraged.

Upvotes: 2

Related Questions