Showing posts with label Code Sample. Show all posts
Showing posts with label Code Sample. Show all posts

Saturday, May 21, 2016

Positioning a control on the center of the form

Sometimes when I created a resizable or maximized form, I want to position my control on the center of my form depending on the screen resolution.

I use this code:
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    Dim btn = New Button()
    Controls.Add(btn)
    btn.Location = New Point((ClientSize.Width - btn.Width) \ 2, _
                             (ClientSize.Height - btn.Height) \ 2)
End Sub
Reference: Centering (perfectly) dynamically created buttons on form?