Center your form on the screen

Level:
Level1

Below you will find only four lines of code. But these four lines of code allow you to do something that would take hundreds of lines of code using a language such as C++. This source sample demonstrates how you can use Visual Basic's built in property called Screen to grab information about, wouldn't you know it, the screen that your application is running on. What this code does is queries the screen width and height. It then subtracts your forms width and height and divides it by two so you can put it in the middle.

To use this source code simply create a new VB project. Open the code for your form and add the following source to it.

  1. With Form1
  2.     .Top = (Screen.Height - .Height) / 2
  3.     .Left = (Screen.Width - .Width) / 2
  4. End With

You will notice that we are using the With construct that VB provides. All this does is causes Form1 to be implied before any . found in the code.

Note: The source for this was found at DreamVB which is no longer online.

If you enjoyed this post, subscribe for updates (it's free)

gud

gud

Great Code

Nice Articles for beginners.

08054034588

Pretty code

Good code. I'd find it by

Good code. I'd find it by myself, when experimenting with Screen object.
What is good - it will pose the form on the center on any screen resolution - so, the form will appear in center and on a small netbook, and on a big screen of a PC.

So good. ... Keep working...

So good. ... Keep working...

do you just copy & paste this

do you just copy & paste this coding?

Awesome

You are right about how difficult this would be in C++. I can't even imagine! Well I can but yeah...

Nice

Nice Thanks
asp.akhilesh@yahoo.co.in