Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))

Problem

You want a form to be in the center of the display when it first appears.

Solution

Set the form's StartPosition property to CenterScreen.

Discussion

That was easy, but there may be cases where you need to set this property to Manual, but you still want the form to appear centered sometimes. To accomplish this, add the following code to the Load event handler for your form:

Me.Location = New Point(( _ Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2, _ (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2)

Категории