Get Resolution of Monitor 2

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 890 Views - Last Post: 28 June 2014 - 04:05 PM Rate Topic: -----

#1 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Get Resolution of Monitor 2

Posted 27 June 2014 - 12:22 PM

I would first of all like thank everyone here for all the help I have gotten learning VB.Net. My knowledge has grown by leaps and bounds thanks to this community.

I need my program To detect the resolution of multiple monitors on a system. I have figured out to get the resolution of one monitor.

Public Function ScreenResolution() As String
        Dim resX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim rexY As Integer = Screen.PrimaryScreen.Bounds.Height
        Return resX & " X " & resY
End Function



And I think I am pretty close to being able to get two or more monitors. I am believe I need to use something like this:

Public Function ScreenResolution() As String
       DIM resX As Integer = Screen.AllScreens(0).length
       DIM resy As Integer = Screen.AllScreens(0).Width
       Return resX & " X " & resY
End Function



However, its not working. I know I haven't quite nailed down the correct syntax for AllScreens. I have been trying to get this right all night. Any help would be appreciated.

Rab

This post has been edited by Rabastan: 27 June 2014 - 12:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Get Resolution of Monitor 2

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 12:29 PM

I don't have multiple screens at the moment but I'll guess it should be:
Public Function ScreenResolution() As String
       DIM resX As Integer = Screen.AllScreens(0).Bounds.Width
       DIM resy As Integer = Screen.AllScreens(0).Bounds.Height
       Return resX & " X " & resY
End Function




Please note that you should provide details about what happens with your code, or what errors are given.
Was This Post Helpful? 2
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,808
  • Joined: 12-June 08

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 12:35 PM

Andrew's right.

Eyeball "AllScreens" for a minute. Realize it is a collection. How do you flip through collections? With a loop!

Intellisense (or msdn) will point you to the properties and functions available to a 'windows.forms.screen' object.. and you should determine that 'bounds' is an ideal object to get the height and width.

  For Each temp As Windows.Forms.Screen In Screen.AllScreens
            Console.WriteLine(String.Format("{0} x {1}", temp.Bounds.Width, temp.Bounds.Height))
        Next


Was This Post Helpful? 3
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 12:43 PM

Hi,
I just wanted to add that you should be converting the (resX) and (resY) to a string using the .ToString method when your adding them into the return string. They are integers. I would highly recommend to turn on Option Strict. You can do that in the Tools/Options menu or you can place "Option Strict On" as the very first line in your code.
Return resX.ToString & " X " & resy.ToString

Was This Post Helpful? 1
  • +
  • -

#5 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 01:42 PM

Thank yo all for your responses, Andrew you code works great. Thank You.

I have a question for IronRazor. As I said I VERY new to VB. It was my understanding that I would only use .ToString if I planed on displaying the returned value. As I plan on comparing it ie:
    If resX < 1028 Then



Wouldn't have to be an integer for the comparison?

Rab

This post has been edited by Rabastan: 27 June 2014 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 01:49 PM

Also, this is my absolute first use of functions.

how would I go about calling this function in a click event. I am basically comparing it to 1920 x 1080 and displaying a message box to say if it qualifies or not.

And lastly is there a way to include the monitor i am checking in the parameters of the function, for example: ScreenResolution(0) for monitor one, ScreenResolution(1) for monitor 2, ect or would I have to create a separate function for each monitor?

Rab
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 01:58 PM

If you will be comparing resX (or resY) within the function then yes, at that point, they would need to be integers.

Your function, though, returns a single string. If you intend to compare the x and y-values outside of the function you you'll (currently) have to split this string to extract (again) the x and y values.

An alternative is to return the Bounds instance itself (a Rectangle), which would be consistent with the name of your function - ScreenResolution():
    Public Function ScreenResolution() As Rectangle
        Dim resX As Integer = Screen.AllScreens(0).Bounds.Width
        Dim resy As Integer = Screen.AllScreens(0).Bounds.Height
        'Return resX & " X " & resy
        Return Screen.AllScreens(0).Bounds
    End Function

This code will only return information about the first screen. I am guessing that you might want to revise the function so that you pass a Screen-index as parameter and it returns the Bounds for that Screen. But this is just a guess. Added: I've just noticed that this is what you suggested in the post you've just made.

View PostRabastan, on 27 June 2014 - 08:49 PM, said:

how would I go about calling this function in a click event.

In the Click event:
    someVariable = ScreenResolution()
    'or
    someVariable = ScreenResolution(0)

You need to do some studying on using functions and function parameters.

This post has been edited by andrewsw: 27 June 2014 - 01:54 PM

Was This Post Helpful? 2
  • +
  • -

#8 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 02:08 PM

Andrew,

My guess was that Screen.AllSreen(0) equals Monitor 1, Screen.AllScreen(1) equals Monitor 2 and so on.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 02:10 PM

..sounds logical. Although you mean AllScreens.

This post has been edited by andrewsw: 27 June 2014 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 02:41 PM

Yah, That too, LOL
:bigsmile:
Was This Post Helpful? 0
  • +
  • -

#11 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 04:44 PM

Hi,
Sorry i missed your question. I was busy helping someone else for a little bit. However, it looks like andrewsw has answered it. You can make your function return the Bounds or a Size of the screens. The Bounds is probably just as easy and would also include the X and Y location along with the Width and Height of the screen.

And yes, (0) would be the 1st screen, (1) the 2nd screen, and so on. B)
Was This Post Helpful? 0
  • +
  • -

#12 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Re: Get Resolution of Monitor 2

Posted 27 June 2014 - 08:38 PM

OK Guys thanks SO Much for all of your help. Got it up and running. Here is the Final Code for this portion.

I have a functions file with:
Public Class clsFunctions

    'Declare Monitor 1 Resolution Function"
    Public Function ScreenResolution_0() As String

        Dim resX_0 As Integer = Screen.AllScreens(0).Bounds.Width
        Dim resY_0 As Integer = Screen.AllScreens(0).Bounds.Height
        Return resX_0 & " X " & resY_0

    End Function

    'Declare Monitor 2 Resolution Function"
    Public Function ScreenResolution_1() As String

        Dim resX_1 As Integer = Screen.AllScreens(1).Bounds.Width
        Dim resY_1 As Integer = Screen.AllScreens(1).Bounds.Height
        Return resX_1 & " X " & resY_1

    End Function

End Class



And in the Main File I have:
Private Sub btnResComply_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim numberofmonitors As Integer = Screen.AllScreens.Length
        If numberofmonitors = 1 Then
            MessageBox.Show("Program will not function correctly using only one monitor." & vbCrLf & "Program Will Now Close")
            Me.Close()
        ElseIf numberofmonitors > 1 Then

            Dim SrnRes As New clsFunctions
            If SrnRes.ScreenResolution_0 <> "1920 X 1080" Then
                MessageBox.Show("Resolution of Monitor 1 is " & SrnRes.ScreenResolution_0.ToString)
                Me.Controls.Remove(btnResComply)
                Me.Controls.Add(btnAdjRes)
                AddHandler btnAdjRes.Click, AddressOf btnAdjRes_Click
            Else
                If SrnRes.ScreenResolution_1 <> "1920 X 1080" Then
                    MessageBox.Show("Resolution of Monitor 2 is " & SrnRes.ScreenResolution_1.ToString)
                    Me.Controls.Remove(btnResComply)
                    Me.Controls.Add(btnAdjRes)
                    AddHandler btnAdjRes.Click, AddressOf btnAdjRes_Click
                Else
                    Me.Controls.Remove(btnResComply)
                    Me.Controls.Remove(rtfResComply)
                    Me.Controls.Add(rtfVpnNac)
                    Me.Controls.Add(btnLnchNac)
                    AddHandler btnLnchNac.Click, AddressOf btnLnchNac_Click
                End If

            End If

        End If

    End Sub



As always Critiques or ways I could have done it better are always welcome. Bear in mind this is the very first time I eve used a function or an If statement.

thanks Again
Rab
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: Get Resolution of Monitor 2

Posted 28 June 2014 - 04:24 AM

Hi,
That`s not bad for your first time making a Function. :)/>

I do see an easy way you can improve the Function so you only need 1 Function to return the size of any screen. You can just add a Parameter in the function so you can pass it the number of the monitor you want the size of kind of like this.
Public Class clsFunctions

    Public Function GetScreenSize(ByVal ScreenNumber As Integer) As String '<-- this function returns a string
        Dim resX_0 As Integer = Screen.AllScreens(ScreenNumber).Bounds.Width
        Dim resY_0 As Integer = Screen.AllScreens(ScreenNumber).Bounds.Height
        Return resX_0.ToString & " X " & resY_0.ToString '<-- the integers should be converted to a string
    End Function

End Class



Then in your Forms code you can call the function with the monitor number and return that monitors size kind of like this.
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Screen.AllScreens.Length < 2 Then
            MessageBox.Show("Less than 2 monitors where detected.")
        Else
            Dim clsf As New clsFunctions

            If clsf.GetScreenSize(0) <> "1024 X 768" Then
                MessageBox.Show("Screen 1 size : " & clsf.GetScreenSize(0)) '<-- the function returns a string so you don`t need .YoString here

                'Do whatever you need here when the 1st monitor is not the required size

            ElseIf clsf.GetScreenSize(1) <> "1024 X 768" Then
                MessageBox.Show("Screen 1 size : " & clsf.GetScreenSize(1)) '<-- the function returns a string so you don`t need .YoString here

                'Do whatever you need here when the 2nd monitor is not the required size

            End If

        End If
    End Sub
End Class




There are probably other (better) ways to do what you want but, it is best to experiment with your code and get to understand how everything works first. You can also read the Microsoft link below to get a better understanding of using Functions.
Function Statement (Visual Basic)

This post has been edited by IronRazer: 28 June 2014 - 04:26 AM

Was This Post Helpful? 1
  • +
  • -

#14 Rabastan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 60
  • Joined: 27-August 12

Re: Get Resolution of Monitor 2

Posted 28 June 2014 - 02:08 PM

Iron Razor,

Again Thank you so much, Here is what I ultimately ended up with.

Public Function GetScreenSize(ByVal ScreenNumber As Integer) As String
        Dim resX As Integer = Screen.AllScreens(ScreenNumber).Bounds.Width
        Dim resY As Integer = Screen.AllScreens(ScreenNumber).Bounds.Height
        Return resX.ToString & " X " & resY.ToString '
    End Function



I can't thank you enough for this!! Because in the future I may add functionality for more monitors. This makes it SO MUCH simpler. I already had Function for five monitors built just in case.

I also changed ResX_0 and ResY_0 to simply ResX and ResY, because I only had the _0 there to identify the separate Functions for Multiple monitors, ie: ResX_0, ResX_1 and so on.

Lastly, again you are advising I convert the resolution to strings in the first function, don't get me wrong I am by no means arguing, clarifying so i know i am doing it right. You obviously have MUCH more experience than I do. It was my understanding that I had to leave them as integers in order to compare them, that I couldn't compare them as a string because all comparisons were considered "mathematical" other than = and <>. So if I am correct in this understanding?

Private Sub btnResComply_Click(ByVal sender As Object, ByVal e As EventArgs)

        If Screen.AllScreens.Length < 2 Then
            MessageBox.Show("Program will not function correctly using only one monitor." & vbCrLf & "Program Will Now Close")
            Me.Close()
        Else
            Dim clsf As New clsFunctions

            If clsf.GetScreenSize(0) <> "1920 X 1080" Then
                MessageBox.Show("The resolution of Monitor 1 is set to " & clsf.GetScreenSize(0) & " At the present time this program will only run at a Resolution of 1920 X 1080, on Two or more monitors. Please adjust your monitors resolution and re-launch the program, or keep checking back, as we will be releasing updates for other resolutions soon")
                Me.Close()
            ElseIf clsf.GetScreenSize(1) <> "1920 X 1080" Then
                MessageBox.Show("The resolution of Monitor 2 is set to " & clsf.GetScreenSize(1) & " At the present time this program will only run at a Resolution of 1920 X 1080, on Two or more monitors. Please adjust your monitors resolution and re-launch the program, or keep checking back, as we will be releasing updates for other resolutions soon")
                Me.Close()
            Else
                Me.Controls.Remove(btnResComply)
                Me.Controls.Remove(rtfResComply)
                Me.Controls.Add(rtfVpnNac)
                Me.Controls.Add(btnLnchNac)
                AddHandler btnLnchNac.Click, AddressOf btnLnchNac_Click
            End If

        End If

    End Sub



What I would eventually like to do is have the message box not close, and revert back to the original form where there is a button that will adjust the resolution for them or throw a message saying there system does not support the resolution. But I am sure thats a step that WAY over my head at this point. So on to the next step. Checking the screen saver timeout.

Again thanks so much, both IronRazor and Andrew. YOu have both been a HUGE help and have taught me alot. I hope one day to be able to give back and help a Uber Newb like myself in here :bigsmile:

Rab
Was This Post Helpful? 2
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,031
  • Joined: 12-December 12

Re: Get Resolution of Monitor 2

Posted 28 June 2014 - 02:39 PM

Quote

..again you are advising I convert the resolution to strings in the first function.. It was my understanding that I had to leave them as integers in order to compare them, that I couldn't compare them as a string because all comparisons were considered "mathematical" other than = and <>. So if I am correct in this understanding?

I am not certain what you are asking here, but if your function returns a string such as "1920 X 1080" then in order for you to subsequently compare these numbers you will first have to break apart this string (using Split()). Once you've split the string into three parts you will also need to convert "1920" to an integer 1920 before you can compare it to another integer value. It's a little messy, but not too tricky.

If, instead, your function returns the Bounds (a Rectangle, as I demonstrated earlier) then you already have the numerical values available as Bounds.Height and Bounds.Width. If, having used your function, you still wish to print out a string representation of the dimensions ("1920 X 1080") you can do so in the same way that you are currently doing within the function.

Essentially, it is your decision as to whether your function should return a single string or a Bounds object. Either way can work.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2