Solved

Centering Forms on any screen size.

Posted on 2006-06-21
6
172 Views
Last Modified: 2010-04-23
This question is worth 250 points and they will be awarded to the person that provides the best solution.

I have an application containing a Form that changes sizes depending on user input. At load the Form is  3" high X 2" wide and if the user clicks a check box on the Form it changes to 3" high X 6" wide. Once the user inputs some info into the Form the Form returns to the 3" X 2" size. My problem is positioning the Form in the center on load for different screen sizes. If I use a laptop with a wide screen it loads differently that if I use a desktop with a 17" monitor. Needless to say that using the automatic "Center Form" property does not work on the wide screen for some reason. The code I'm using to try and get around this is below:

Me.Size = New Size(m_intX1, m_intY)
Dim intX As Integer = 0
Dim intY As Integer = 0
intX = (Me.ClientSize.Width / 2)               ' Adjust Form to center of screen
intY = (Me.ClientSize.Height / 2)              ' Adjust Form to center of screen
Me.Location = New Point(intX, intY)          ' Relocates the Form to the center of ANY screen  

However, this doesn't seem to work for every case and different screen sizes and shapes. Any thoughts?

Thanks in advance for your help.
0
Comment
Question by:Mr_Fulano
6 Comments
 
LVL 2

Accepted Solution

by:
cwile earned 250 total points
ID: 16950862
Have you tried just adding this to the places you wish the form to be centered?

Me.CenterToScreen()

I have a 17" widescreen laptop and that works just peachy for me.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16950863
Me.CenterToScreen()
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16950875
cwile, you beat me by couple of seconds:)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16950885
Hi,

try like this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StartPosition = FormStartPosition.CenterScreen
    End Sub

regards
pradeep
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 16951385
Hi,

try like this


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2

 End Sub

regards
pradeep
0
 

Author Comment

by:Mr_Fulano
ID: 16973589
Everyone contributed very nicely, however, cwile came up with a solution that worked first, so I awarded him the points. I appreciate all your help.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now