Thursday, July 23, 2009

Align the Form in a screen based on screen resolution

The following VB.NET Code Snippet is used to dynamically align the form to the center of the window, based on the the end users screen resolution.

Sub Center_Form()
Dim maxwidth As Integer = Screen.PrimaryScreen.WorkingArea.Size.Width
Dim left As Integer
Dim top As Integer
'Remove 10% top part of window
Dim maxheight As Integer = Screen.PrimaryScreen.WorkingArea.Size.Height - Screen.PrimaryScreen.WorkingArea.Size.Height * 0.1
left = (maxwidth - Me.Width) / 2
top = ((maxheight - Me.Height) / 2)
Me.Location = New Point(left, top)
End Sub


Call the function Center_Form in the Form_Load event of the form.

Private Sub DataForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Center display
Center_Form()
End Sub


The following VB.NET Code Snippet is used to maximize the form.

Private Sub Maximize()
Me.WindowState = FormWindowState.Maximized
End Sub


The following VB.NET Code Snippet is used to minimize the form.

Private Sub Minimize()
Me.WindowState = FormWindowState.Minimized
End Sub

No comments: