Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Center Group box on form load

Center Group box on form load

Scheduled Pinned Locked Moved Visual Basic
help
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hkinser9
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • H hkinser9

      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

      A Offline
      A Offline
      Are Jay
      wrote on last edited by
      #2

      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.

      H T 2 Replies Last reply
      0
      • A Are Jay

        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.

        H Offline
        H Offline
        hkinser9
        wrote on last edited by
        #3

        That did it!! Thank you very much. - Harold

        1 Reply Last reply
        0
        • A Are Jay

          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.

          T Offline
          T Offline
          The ANZAC
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • T The ANZAC

            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

            A Offline
            A Offline
            Are Jay
            wrote on last edited by
            #5

            Thanks. Do you know of a property for the client area only?

            T 1 Reply Last reply
            0
            • A Are Jay

              Thanks. Do you know of a property for the client area only?

              T Offline
              T Offline
              The ANZAC
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups