Dnakk Jam
Dnakk Jam

Reputation: 371

load form half screen in vb.net

I would like to dock my form to the right (max resolution height and max resolution width / 2). I am using VB.NET and my code so far is:

  Dim scr As Screen = Screen.FromControl(Me)
  Me.Top = scr.WorkingArea.Top + scr.WorkingArea.Height - Me.Height
  Me.Left = scr.WorkingArea.Left + scr.WorkingArea.Width - Me.Width

Any ideas how to make my form half as big as my current resolution and to position it at the right side of the desktop (like when using windows-key + right arrow)?

Upvotes: 2

Views: 1982

Answers (1)

Hans Passant
Hans Passant

Reputation: 941218

You'll need to set the Width and Height as well, not just the position. Be sure to do this after the window has been rescaled, the form's Load event is best:

Public Class Form1
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        Dim work = Screen.FromControl(Me).WorkingArea
        Me.Top = work.Top
        Me.Left = work.Right - work.Width / 2
        Me.Width = work.Width / 2
        Me.Height = work.Height
        MyBase.OnLoad(e)
    End Sub
End Class

If you do this after the window has already been displayed then favor assigning the Bounds property instead, it will avoid the repaints.

Upvotes: 4

Related Questions