Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am working with an application that will have sub forms. The primary form opens above the system tray, and on mouse-over, secondary windows will open right next to the primary. When I use the Me.location = New Point () and then assign the point, it works great… until I change PC's and the user has a different screen resolution.

I was thinking of something like this:

Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
Dim errMsg As String = "An Error has occured. Please close the app and contact Jeremy." + Environment.NewLine + "Error 101: Missing Resolution " + Str(intX) + " x " + Str(intY) + "."
MsgBox(errMsg, vbCritical)

Select Case intX
    Case "1920"
        If intY = "1200" Then
            'Me.Location = New Point
        Else
            MsgBox(errMsg, vbCritical)
        End If
    Case "1650"
        If intY = "1200" Then
            'Me.Location = New Point
            MsgBox(errMsg, vbCritical)
        Else
            MsgBox(errMsg, vbCritical)
        End If
    Case "1440"
        If intY = "900" Then
            'Me.Location = New Point
        Else
            MsgBox(errMsg, vbCritical)
        End If
    Case "1280"
        Select Case intY

            Case "1024"
                'Me.Location = New Point
            Case "960"
                'Me.Location = New Point
            Case "800"
                'Me.Location = New Point
            Case "768"
                'Me.Location = New Point
            Case Else
                MsgBox(errMsg, vbCritical)
        End Select
End Select

But I am confident there is a cleaner, faster way to determine the screen resolution and decide placement of the secondary windows.

share|improve this question

migrated from stackoverflow.com Apr 29 at 11:44

1 Answer

Why don't you simply subtract the width and height of your form from the value obtained by the Screen class?

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

SecondForm.Location = new Point(intX - SecondForm.Width, intY - SecondForm.Height)

also, to keep in consideration the effective area available for your forms (exluding the app bar) you could use the Screen.PrimaryScreen.WorkingArea property instead of Bounds

If the position of the second form should be dinamically calculated from the position of the first form, then you need to use the location of the first form and adding its width to place the second one

SecondForm.Location = new Point(FirstForm.Location.X + FirstForm.Width, FirstForm.Location.Y)

REMEMBER. To set a custom location for a form it is necessary to set also the property Form.StartPosition to FormStartPosition.Manual

Some introductory docs on Form Location here

share|improve this answer
much nicer. Can you elaborate on what it means? I have used this code, or very similar to open the initial window, but I dont fully understand what is going on here. And to prevent me from needing to ask more questions from just copying and pasting the code, I would like to understand it. – Phil J Fry Apr 28 at 0:08
1  
Well, knowing your limits on the X/Y axys you stay inside the screen subtracting the width and height of your form. Of course, if the position of the second form should be dinamically calculated from the position of the first form, then you need to use the location of the first form and adding its width to place the second one – Steve Apr 28 at 0:11
@Steve Do not ask users to accept answers, it is considered noise and will be deleted (and further action may be taken). Do not do this again. – casperOne Apr 29 at 11:43
@casperOne could you point me to the relevant discussion about this, thanks – Steve Apr 29 at 12:28
It's in the documentation for the privileges on commments. See "when should I comment". None of your comments fall into that category. Also see the section about "when shouldn't I comment", specifically "Discussion of community behavior or site policies". – casperOne Apr 29 at 12:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.