|
-
Dec 17th, 2000, 05:39 PM
#1
Thread Starter
Fanatic Member
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!
-
Dec 17th, 2000, 05:52 PM
#2
Addicted Member
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
-
Dec 17th, 2000, 07:48 PM
#3
Thread Starter
Fanatic Member
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!
-
Dec 17th, 2000, 07:54 PM
#4
Arcom's code is correct as posted.
-
Dec 17th, 2000, 07:55 PM
#5
Hyperactive Member
He is right it doesn't quiet work correctly. Let me take a look at it
-
Dec 17th, 2000, 08:03 PM
#6
Hyperactive Member
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
-
Dec 17th, 2000, 08:46 PM
#7
PowerPoster
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
-
Dec 17th, 2000, 10:34 PM
#8
Hyperactive Member
The code that Arcom post is working, I used it for over a
year. But there will have error if the windowstate is
Maximize
-
Dec 17th, 2000, 11:14 PM
#9
PowerPoster
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
-
Dec 18th, 2000, 02:45 AM
#10
Fanatic Member
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
-
Dec 18th, 2000, 03:11 AM
#11
PowerPoster
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
-
Dec 18th, 2000, 12:34 PM
#12
Fanatic Member
Chemically Formulated As:
Dr. Nitro
-
Dec 18th, 2000, 12:52 PM
#13
_______
<?>
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
-
Dec 18th, 2000, 05:58 PM
#14
Addicted Member
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..."
-
Dec 18th, 2000, 06:33 PM
#15
Fanatic Member
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
-
Dec 18th, 2000, 06:44 PM
#16
_______
<?>
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
-
Dec 18th, 2000, 09:20 PM
#17
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.
-
Dec 18th, 2000, 09:43 PM
#18
PowerPoster
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.
-
Dec 18th, 2000, 09:58 PM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|