Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 363
vulkanino Avatar asked Feb 07 '11 17:02

vulkanino


2 Answers

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.

like image 150
vulkanino Avatar answered Oct 27 '22 20:10

vulkanino


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.

like image 33
Pedery Avatar answered Oct 27 '22 18:10

Pedery