Search code examples
vb.netresizefullscreen

Putting VB.NET Controls in center of a sizable window


I'm creating a program in VB.NET (Microsoft's Visual Basic 2010).
I want to know if I can put all my content in the center of the screen. At the moment it's at the far top-left of the screen.
When the window size varies I want it to stay in the middle for all shapes and sizes. A similar example using HTML and CSS to achieve the above:

<div style="margin: 0 auto; width: 500px;"></div>

Solution

  • You should be able to do it using the Form's Resize event but personally I'd use the TableLayoutPanel control (documentation here).

    Here are the steps:

    1. Put all of your control into a single Panel
    2. Add 3 columns and 3 rows to the TableLayoutPanel
    3. Drag your new Panel into the center.
    4. Dock the table so that it fills the whole Form.
    5. Set all of the rows'/columns' size to 33.33%, this should now mean that your content will always be centered.