Results 1 to 19 of 19

Thread: Centering Form

  1. #1

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619

    Question

    HOw can i always center the form even if i adjust the height in runtime, how can it always be in center?

    If a post has helped you then Please Rate it!

  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    I assume you want to center it on the screen.
    Here goes:

    Private Sub Form_Resize()
    Form1.Left = (Screen.Width - Form1.Width) / 2
    Form1.Top = (Screen.Height - Form1.Height) / 2
    End Sub

  3. #3

    Thread Starter
    Fanatic Member VBKNIGHT's Avatar
    Join Date
    Oct 2000
    Location
    Port25
    Posts
    619
    i tried using your code but it doesnt work!
    it goes all the way to the bottom.

    If a post has helped you then Please Rate it!

  4. #4

  5. #5
    Hyperactive Member PITBULLCJR's Avatar
    Join Date
    Nov 1999
    Location
    New York
    Posts
    408
    He is right it doesn't quiet work correctly. Let me take a look at it
    Sincerely,
    Chris


    Email: [email protected]
    AIM: KnightsOfTheMoon
    WebPage: http://kom.wicre.com
    ----------------
    VB6 Professional
    Abit ST6-RAID
    1000 MHZ
    512 MB PC133 Ram
    Nvidia GeForce 2 Ultra 64 MB
    Maxtor 81.9 Gig
    Win 98 SE

  6. #6
    Hyperactive Member PITBULLCJR's Avatar
    Join Date
    Nov 1999
    Location
    New York
    Posts
    408

    Talking Well....

    Code:
    Form1.Top = (Screen.Height * 0.85) \ 2 - Form1.Height \ 2
    Form1.Left = Screen.Width \ 2 - Form1.Width \ 2
    That is the way you should center the form given to me by the poeple who made vb. Umm heres the thing though. It seems when you put it in resize it doesn't always work right. So what i did was put it in a timer then it works. So i set the timer to interval 1 and always true. I hope this helps
    Sincerely,
    Chris


    Email: [email protected]
    AIM: KnightsOfTheMoon
    WebPage: http://kom.wicre.com
    ----------------
    VB6 Professional
    Abit ST6-RAID
    1000 MHZ
    512 MB PC133 Ram
    Nvidia GeForce 2 Ultra 64 MB
    Maxtor 81.9 Gig
    Win 98 SE

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    Well, the code that Arcom posted is working. Perhaps you can use this instead...

    Code:
    '* Me insteaed of Form1
    '* / instead of /
    '  Because the \ operator will return the interger value and ignore the decimal point
    
    Me.Top = (Screen.Height - Me.Height) \ 2
    Me.Left = (Screen.Width - Me.Width) \ 2

  8. #8
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318

    Cool

    The code that Arcom post is working, I used it for over a
    year. But there will have error if the windowstate is
    Maximize

  9. #9
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    kmchong, your're correct, hence the code should look like this

    Code:
    If Me.WindowState <> vbMinimized And Me.WindowState <> vbMaximized Then
        Me.Top = (Screen.Height - Me.Height) \ 2
        Me.Left = (Screen.Width - Me.Width) \ 2
    End If

  10. #10
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    VBKNIGHT is right. The code and formula posted maybe correct, but it doesn't work correctly in the following example.
    Code:
    Private Sub Form_Resize()
      Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    End Sub

    You might have to do something like this.
    Code:
    Private Sub Timer1_Timer()
      Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  11. #11
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    Nitro, The code doesn't work correctly it because the code itself does not verified the WindowState before execute it.

    As kmchong mention, we should check the WindowState before we procedd to resize the form. That is when thewindow is in either Maximized or Minimized condition, the resize code should not be executed else, runtime error will occur.

    Yet, for this task, it is need to to applied a Timer control which will eat another portion of the CPU process cycle.

    regards,
    Chris.C

  12. #12
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Thanks Chris
    Chemically Formulated As:
    Dr. Nitro

  13. #13
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'center your form on load and resize
    Option Explicit
    
    Private Sub Form_Activate()
        Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    End Sub
    
    Private Sub Form_Resize()
        Call Form_Activate
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  14. #14
    Addicted Member
    Join Date
    Aug 2000
    Location
    Croatia
    Posts
    200
    Using a timer is not a very good idea, because then the user wouldn't be able to move the form to another position.
    It will ALWAYS move to center, and that is not a very "user friendly". "Trust me, I know what I'm doing..."

  15. #15
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    This doesn't work as you resize the form.

    Private Sub Form_Resize()
    Form1.Left = (Screen.Width - Form1.Width) / 2
    Form1.Top = (Screen.Height - Form1.Height) / 2
    End Sub

    I agree a timer control is not efficient.
    Chemically Formulated As:
    Dr. Nitro

  16. #16
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    The code I pasted works. It's where you put it. No timer required.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  17. #17
    Guest

    Question This might be a silly question but why aren't you just

    1. Click on the form.

    2. Go to the form properties.

    3. Click on "StartupPosition".

    4. Choose "2. CenterScreen".

    We have yet to find a PC where our deployment doesn't work using this approach.

  18. #18
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Jethro, set the StartupPosition only valid for those form loaded at first time. If the user move it or resize it during runtime, it will not vbe able to reposition the at center of the screen.

  19. #19
    Guest
    Originally posted by Chris
    Jethro, set the StartupPosition only valid for those form loaded at first time. If the user move it or resize it during runtime, it will not vbe able to reposition the at center of the screen.
    Hmmm....surely that is how a windows product works. You can resize and move forms to your hearts content, without the program helping you out by centering the form each time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width