ur_Auror
ur_Auror

Reputation: 103

Center form on screen during runtime

I'm using vb.net, and what I have in mind is on clicking a button, the current form will expand and it will be centered on screen.

I've figured out how to expand the form, but I can't center it on screen.

I've tried using this code but it's not what I wanted to get.

Dim Width As Integer = Screen.PrimaryScreen.Bounds.Width
Dim Height As Integer = Screen.PrimaryScreen.Bounds.Height

Me.Location = New Point(Height / 2, Width / 2)

or otherwise: Me.Location = New Point(Width / 2, Height/ 2)

even tried this: Me.StartPosition = FormStartPosition.Center

Upvotes: 0

Views: 3906

Answers (1)

Subaz
Subaz

Reputation: 917

Use .CentreToScreen()

Like this:

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Me.Size = New System.Drawing.Size(400, 650)
    Me.CenterToScreen()
End Sub

Upvotes: 1

Related Questions