I have an issue with WinForms (VB.NET).
The main window is an MDI container. The user opens a new child window:
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:
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!
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)!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With