Center Group box on form load
-
All, I've created a timer program that when it ends, displays a password form (no border) via showdialog that is maximized when displayed. The form contains one group box that holds my text box to enter the psw in and an enter button to confirm. My problem is that I won't know what resolution a person screen may be set to and when the form loads on their PC, I want the group box to be centered in the middle of the form to ensure it will always be visible. I know it sounds simple (probably is), but I can't seem to get the group box to center when I try different resolutions. My fear is that if I don't get it to center, it may very well show too far off to one side on someones computer and they will not be able to do anything but shutdown. Plus centered looks must more professional as well. All help is greatly appreciated! Thanks, everyone. - Harold
-
All, I've created a timer program that when it ends, displays a password form (no border) via showdialog that is maximized when displayed. The form contains one group box that holds my text box to enter the psw in and an enter button to confirm. My problem is that I won't know what resolution a person screen may be set to and when the form loads on their PC, I want the group box to be centered in the middle of the form to ensure it will always be visible. I know it sounds simple (probably is), but I can't seem to get the group box to center when I try different resolutions. My fear is that if I don't get it to center, it may very well show too far off to one side on someones computer and they will not be able to do anything but shutdown. Plus centered looks must more professional as well. All help is greatly appreciated! Thanks, everyone. - Harold
Private Sub Form1_ResizeBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize Dim x As Integer = CInt((Me.Size.Width / 2) - (Me.GroupBox1.Width / 2)) Dim y As Integer = CInt((Me.Size.Height / 2) - (Me.GroupBox1.Height / 2)) Me.GroupBox1.Location = New Point(x, y) End Sub
The height seems to be offset, but this might get you closer to your answer. -
Private Sub Form1_ResizeBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize Dim x As Integer = CInt((Me.Size.Width / 2) - (Me.GroupBox1.Width / 2)) Dim y As Integer = CInt((Me.Size.Height / 2) - (Me.GroupBox1.Height / 2)) Me.GroupBox1.Location = New Point(x, y) End Sub
The height seems to be offset, but this might get you closer to your answer. -
Private Sub Form1_ResizeBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize Dim x As Integer = CInt((Me.Size.Width / 2) - (Me.GroupBox1.Width / 2)) Dim y As Integer = CInt((Me.Size.Height / 2) - (Me.GroupBox1.Height / 2)) Me.GroupBox1.Location = New Point(x, y) End Sub
The height seems to be offset, but this might get you closer to your answer. -
The height will be offset because you have to accomodate for the titlebar(which is normally 30 pixels) and the bottom edge(which is normally 3-4 pixels).
Posted by The ANZAC
-
Ok, i've come up with this:
Dim y As Integer = Me.Bounds.Height / 2 - Me.GroupBox1.Height / 2 Dim x As Integer = Me.Bounds.Width / 2 - Me.GroupBox1.Width / 2 Me.GroupBox1.Location = New Point(x, y)
This will center your control, in this case, a groupbox within the forms client area. However ther may be one drawback (depending on how you see it). If you have a docked menu at the top of your form, the control will center itself between the bottom of the form and the menu. I'm guessing the same goes for any docked menu on any side. Other than that, i think this works quite well.Posted by The ANZAC