HOw can i always center the form even if i adjust the height in runtime, how can it always be in center?
Printable View
HOw can i always center the form even if i adjust the height in runtime, how can it always be in center?
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
i tried using your code but it doesnt work!
it goes all the way to the bottom.
Arcom's code is correct as posted.
He is right it doesn't quiet work correctly. Let me take a look at it
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 helpsCode:Form1.Top = (Screen.Height * 0.85) \ 2 - Form1.Height \ 2
Form1.Left = Screen.Width \ 2 - Form1.Width \ 2
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
The code that Arcom post is working, I used it for over a
year. But there will have error if the windowstate is
Maximize
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
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
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
Thanks Chris
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
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..." :)
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.
The code I pasted works. It's where you put it. No timer required.
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.
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.Quote:
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.