How to get the Computer Screens Resolution using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.20 out of 5)
 

At some point you may need to get a computers monitor/video resolution for sizing your applications and/or your applications controls/fonts or whatever. It is actually easy to do so in ALL versions of Visual Basic. Only a very small amount of additional code is required to get the Resolution in 6.0 compared to doing it in .NET. Below is doing it in VB 6.0.


Visual Basic 6.0

To get the resolution in VB Classic is quite easy. You can use the following code below…

Screen.Width – For the Horizonal Resolution.

Screen.Height – to get the Vertical resolution.

But if you use those codes by themselves, it will not return the resolution in Pixels which is probably the format you are wanting. There is an extra piece of code for getting the resolution in Pixels…

Screen.Width / Screen.TwipsPerPixelX

Screen.Height / Screen.TwipsPerPixelY

Below is the code put together with a Messagebox displaying the resolution for the screen…

'
'Simply throws a messagebox with the current resolution of the screen...
MsgBox("The Screen Resolution is: " & Screen.Width / Screen.TwipsPerPixelX & "x" & Screen.Height / Screen.TwipsPerPixelY)

What about the VB.NET versions? Even easier actually.


Visual Basic.NET & VB 2008/2010, and VB 2013

What we want is the Screen class located at:

System.Windows.Forms.Screen

This handy little Class has data related to the computers display screen. Below is a simply example…

'Simply display a messagebox with the resolution of the computers screen.
MsgBox(Screen.PrimaryScreen.Bounds.Size.ToString)

If a computer has more than one(1) screen/monitor, then you could also use the ‘Screen’ class to get the resolution for that screen.

Example: Say your application was running on a computer with Two(2x) monitors/displays and you wanted to know the resolution of the monitor your Form is shown on or the monitor that is showing the biggest portion of your Form? The Screen Class can help you with that…

'
'If you had multiple displays, and you wanted to know the resolution of the display
' that contains your form, this code would do that for you.
MsgBox(Screen.FromControl(Me).Bounds.Size.ToString)

 

In Visual Basic 2005 and/or Visual Basic 2008, VB 2010, and newer you can even use the MY interface to access the Screen class.

Example…

'
'Use the MY shortcut in VB 05/08 to get the sacreen size...
MsgBox(My.Computer.Screen.Bounds.Size.ToString)

Well, thats all there is to it if you wanted to get the Computers Display/Screen Resolution in your Applications. Its easy to do from Visual Basic 5.0/6.0 through to Visual Basic 2005/2008. Have fun! 🙂

Jason

10 thoughts on “How to get the Computer Screens Resolution using VB and VB.NET

  1. TomboFry

    Is there a way to change the screen resolution only when you’re running the application, and when you exit, it goes back to normal?
    Reply would be highly appreciated. Thanks.

    Reply
  2. Ahmad

    Its not actually adjusting the form height and width with its control according to the form resizing or maximising help!!!!

    Reply
  3. Muhammad Shahid

    I designed my VB program on a laptop with a screen resolution of 1280 x 800. All forms look great. But when I install the same VB program on a computer with screen resolution of 1024 x 768, the forms and buttons etc. are too big to show on the screen. Do I have to redesign my program to fit the smaller screen resolution? If I do this, then the forms and buttons etc. will be too small on the larger resolution screen. Any help to solve this matter would be appreciated!

    shahid

    Reply

Leave a Reply