Changing position of Userform at Startup

E

EllenM

I came across the "StartUpPosition Property" which allows the developer to
specify the location of a userform when it first appears onscreen.
Unfortunately, the settings only give you 4 options which do not allow much
positional control of a userform. I have a large form with an embedded button
such that when the button is clicked, it launches a smaller form (a calendar)
in front of the large form. I would like the smaller form to appear slightly
off-center, not dead center. Is there a way to do this?



Thanks'
Ellen
 
J

Jean-Guy Marcil

EllenM said:
I came across the "StartUpPosition Property" which allows the developer to
specify the location of a userform when it first appears onscreen.
Unfortunately, the settings only give you 4 options which do not allow much
positional control of a userform. I have a large form with an embedded button
such that when the button is clicked, it launches a smaller form (a calendar)
in front of the large form. I would like the smaller form to appear slightly
off-center, not dead center. Is there a way to do this?

For something like this you need to first find out what the user's screen
resolution is, otherwise you might position the userform totally off the
screen!

Once you know that, you can calculate where to position the ".Top" and
".Left" proprerties of the userform in its Initialize event.

To get the Screen Resolution:


Dim lngHor As Long
Dim lngVert As Long

With System
lngHor = .HorizontalResolution
lngVert = .VerticalResolution
End With

MsgBox lngHor
MsgBox lngVert


Then you need to apply some percent type of calculation to position the
userform as you wish. Of course, thorough testing is recommended. Change your
screen resolution and then run your code to see the results.
 
H

Harry-Wishes

Jean-Guy Marcil said:
For something like this you need to first find out what the user's screen
resolution is, otherwise you might position the userform totally off the
screen!

Once you know that, you can calculate where to position the ".Top" and
".Left" proprerties of the userform in its Initialize event.

To get the Screen Resolution:


Dim lngHor As Long
Dim lngVert As Long

With System
lngHor = .HorizontalResolution
lngVert = .VerticalResolution
End With

MsgBox lngHor
MsgBox lngVert


Then you need to apply some percent type of calculation to position the
userform as you wish. Of course, thorough testing is recommended. Change your
screen resolution and then run your code to see the results.
Hello,

EllenM and I are working on this project together and perhaps I should
provide some clarification.

First, thanks for the quick reply.

We probably didn't communicate that clearly how the 2 forms interact with
one another. The solution you posted works fine if the small form is embedded
within the larger form. However, we did not design our project that way. We
created 2 separate userforms independent of one another such that, during
runtime, the large form appears onscreen containing a button which, when
pressed, launches a smaller separate window which happens to be another
userform. As you know, all windows be it forms or dialog boxes appear dead
center when they first appear onscreen.



The reason we mentioned the "StartUpPosition" Property is because I was
wondering if you could somehow programmatically bypass what seems to be the
only 4 options for positioning dialog boxes on the screeen. To reiterate, I
want the smaller window to appear off-center to the larger window. See below.
When EllenM and I search VBA help for Word using "StartUpPosition" as the
search term, we retrieved the following. Only 4 options appear. Any help
appreciated. Thanks again.





Manual 0 No initial setting specified.

CenterOwner 1 Center on the item to which the UserForm belongs.

CenterScreen 2 Center on the whole screen.

WindowsDefault 3 Position in upper-left corner of screen.
 
J

Jean-Guy Marcil

Harry-Wishes said:
Hello,

EllenM and I are working on this project together and perhaps I should
provide some clarification.

First, thanks for the quick reply.

We probably didn't communicate that clearly how the 2 forms interact with
one another. The solution you posted works fine if the small form is embedded
within the larger form. However, we did not design our project that way. We
created 2 separate userforms independent of one another such that, during
runtime, the large form appears onscreen containing a button which, when
pressed, launches a smaller separate window which happens to be another
userform. As you know, all windows be it forms or dialog boxes appear dead
center when they first appear onscreen.



The reason we mentioned the "StartUpPosition" Property is because I was
wondering if you could somehow programmatically bypass what seems to be the
only 4 options for positioning dialog boxes on the screeen. To reiterate, I
want the smaller window to appear off-center to the larger window. See below.
When EllenM and I search VBA help for Word using "StartUpPosition" as the
search term, we retrieved the following. Only 4 options appear. Any help
appreciated. Thanks again.





Manual 0 No initial setting specified.

CenterOwner 1 Center on the item to which the UserForm belongs.

CenterScreen 2 Center on the whole screen.

WindowsDefault 3 Position in upper-left corner of screen.

I did understand your question, at least I think did.
Again, to achieve what you want, you cannot use the StartUpPosition
property. You have to use code similar to what I posted.
I did make a mistake...You have to place that code in the Activate event of
the userform, not the Initialize one. Here is an example:


Private Sub UserForm_Activate()

Dim lngHor As Long
Dim lngVert As Long

With System
lngHor = .HorizontalResolution
lngVert = .VerticalResolution
End With

Me.Left = (lngHor / 2.5)
Me.Top = (lngVert / 3)

End Sub
 
H

Harry-Wishes

Jean-Guy Marcil said:
I did understand your question, at least I think did.
Again, to achieve what you want, you cannot use the StartUpPosition
property. You have to use code similar to what I posted.
I did make a mistake...You have to place that code in the Activate event of
the userform, not the Initialize one. Here is an example:


Private Sub UserForm_Activate()

Dim lngHor As Long
Dim lngVert As Long

With System
lngHor = .HorizontalResolution
lngVert = .VerticalResolution
End With

Me.Left = (lngHor / 2.5)
Me.Top = (lngVert / 3)

End Sub

Thanks! Now I am making some progress. While, this works in moving the
window off-center, it does not move the calendar with it. This was the major
element that was placed inside the window. My guess is that it stayed home.
Is there anyway of moving the objects along with the window? Currently, I am
getting the window in this new location with an empty screen.

Harry_Wishes.
 
H

Harry-Wishes

Ahh! ! ! I discovered that when I swap my UserForm_Initialize() and
UserForm_Activate() code so that UserForm_Initialize appears first, I see my
calendar. It works great! ! ! Can't explain why the re-order would solve this
problem, but it works.

Thanks again.

Harrry-Wishes.
 
J

Jean-Guy Marcil

Harry-Wishes said:
Ahh! ! ! I discovered that when I swap my UserForm_Initialize() and
UserForm_Activate() code so that UserForm_Initialize appears first, I see my
calendar. It works great! ! ! Can't explain why the re-order would solve this
problem, but it works.

Glad you got it working... But I must admit I do not understand what you
mean by swapping the two events. When a userform is called into existence,
the Initialize runs first (and only once) and the Activate event runs after
that and everytime the form gets the focus.
Unless you mean you swapped the code from one event to the other...
 
H

Harry-Wishes

Jean-Guy Marcil said:
Glad you got it working... But I must admit I do not understand what you
mean by swapping the two events. When a userform is called into existence,
the Initialize runs first (and only once) and the Activate event runs after
that and everytime the form gets the focus.
Unless you mean you swapped the code from one event to the other...

Yes. I think that is what I meant. I just switched the modules again and do
not see any change at runtime, just as I had originally predicted. Everything
is clear now.

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top