Solved

Centering Forms on any screen size.

Posted on 2006-06-21
6
173 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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