Tuesday, July 21, 2009

Dynamically align the Form in a screen based on users 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()

<B>Me.WindowState = FormWindowState.Minimized</B>

End Sub

3 comments:

  1. hi
    Do i need to add any namespaces?
    if so please tel me

    ReplyDelete
  2. Hi,

    There is no need to add any namespace to use the above code snippet.

    ReplyDelete
  3. hi
    i added the above code in mater page
    I am getting error at this.Width
    do i need to add any namespace

    ReplyDelete