Question A question about form position

borris83

Member
Joined
Apr 30, 2009
Messages
23
Programming Experience
Beginner
Say I have form1 and form2 in an application... There is a button1 which will hide form1 and show form2...

When the form1 appears initially it appears at the left hand side corner of my screen. But when form2 shows, its position is in the right side of the screen.

I noticed that any form which is shown slightly changes its position.. How to fix the position of the form so that all the forms show up in the same position?

I am a beginner and I hope this will be very easy to answer. Thanks in advance
 
Set the form's StartupPosition (I don't remember the name of the property exactly right off hand) to Manual then you can progromatically change the form's location either in it's own Load event or from the owner form right before showing it
 
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StartPosition = FormStartPosition.Manual
        Me.SetDesktopLocation(X As Integer, Y As Integer)
    End Sub

Hope this helps!
 
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StartPosition = FormStartPosition.Manual
        Me.SetDesktopLocation(X As Integer, Y As Integer)
    End Sub

Hope this helps!

Thanks... Can you suggest a value for X and Y to load the form in center.. I am not sure what to put in there
 
It will change on a different computer, depending on the current computer's resolution. You can use: Me.StartPosition = FormStartPosition.CenterScreen or you can use the following code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Formula: (Screen Resolution / 2) - (Form Size  / 2)
        Dim screenHeight As Integer = My.Computer.Screen.Bounds.Height / 2
        Dim screenWidth As Integer = My.Computer.Screen.Bounds.Width / 2
        Dim formWidth As Integer = Me.Width / 2
        Dim formheight As Integer = Me.Height / 2
        Me.SetDesktopLocation(screenWidth - formWidth, screenHeight - formheight)
    End Sub
 
Back
Top