Reputation: 155
How can I arrange controls in my parent form by code?
So far I call the user controls with this code;
Me.ParentForm.Controls.Remove(Me)
controlMain()
I want the user controls arrange itself whenever the user resize the parent form or maximize the form. Currently, I set the controls by,
Public Sub controlMain()
Dim usrctl As New _ctlMain
_Main.Controls.Add(usrctl)
usrctl.Location = New Point(_Main.Width / 2 - usrctl.Width / 2, _Main.Height / 2 -usrctl.Height / 2)
End Sub
which is on a module. _Main is my parent form while _ctlMain is the control being called. I do not intend to put the user control on the parent form during design because I have other user controls to call after a specific function in an active control is called.
I have tried the autosize property of usercontrol but I guess it doesn't work on my application. Usercontrol doesn't have the dock and anchor properties.
Upvotes: 0
Views: 3207
Reputation: 244752
Use one of the automatic layout controls, like a FlowLayoutPanel or a TableLayoutPanel.
Instead of adding your user controls to the form itself, add them to either a FlowLayoutPanel or TableLayoutPanel control that has been placed on top of the form using DockStyle.Fill
.
It sounds to me like a FlowLayoutPanel is what you want. With that, the layout of the controls is handled entirely automatically, and they are positioned either in left-to-right or top-to-bottom order, depending on the value of the FlowDirection
property.
The only reason to choose a TableLayoutPanel is if you need to have more precise control over the exact positioning of the controls. It works just like an HTML table, with each control getting its own "cell".
You can also set the Dock
and/or Fill
properties of the individual user controls if you'd like to ensure that their sizes are automatically adjusted. For example, you can set each control to fill the entire cell in which it is placed in a TableLayoutPanel.
Upvotes: 1