Position of a button in a toolbar

DocNsanE used Ask the Experts™
I need to determine the position of a button in a toolbar relative to the screen. My goal is to open a form directly under the button when the user clicks on it.

I've tried :
Button.Left and Button.Top but they seem to be relative to the usercontrol.. and usercontrol.left or usercontrol.top always return zero.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Is your user control running inside some other window?  Try:
   intLeft = Button.Left + UserControl.Parent.Left
   intTop = Button.Top + UserControl.Parent.Top

Good luck...


Well the usercontrol that I need this code for resides within another usercontrol, which is in development so it has no parent at the moment. But when I try to resolve my first user control's Parent, it returns "Client Site Not Available"... whatever that means!

Thanks for the comment, but if there's a way I could determine and set the position relative to the user's screen, that's what I'd really want.

I probably got this all wrong but:

If you are going to open a button within your activex component with that toolbar, do you need to know the absolute position of the toolbar according to the form which it resides? Couldn't you just position the top of the form after the toolbar button?

posx = button.left + button.width
posy = button.top + button.height

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


No that isn't working...
Button.Left returns the position of the button relative to the usercontrol (the left edge of the usercontrol being 0) and button.top is the same. But frmForm.Left returns the position of the form relative to the screen.

For example, if my usercontrol is at (350px,400px) relative to the screen, and
Button.Left = 40
Button.Top = 0

Then I want to be able to set :
frmForm.Left = 390
frmForm.Top = 400 + Button.Height

It would be as simple as
frmForm.Left = UserControl.ScaleLeft + Button.Left
frmForm.Top = UserControl.ScaleTop + Button.Top _
                + Button.Height

But unfortunatly, UserControl.ScaleLeft and .ScaleTop always return 0.
I guess you could use Win32 API call GetCursorPos

Public Type TPoint
  x As Long
  y As Long
End Type

Public Declare Function GetCursorPos Lib "User32" (ByRef pt As TPoint) As Long

You could use it like..

dim pt as TPoint

call GetCursorPos(pt)

pt.x and pt.y will hold the screen pos in pixels


> I guess you could use Win32 API call GetCursorPos
Yeah that's my temporary solution.. I use GetCursorPos and convert the result into twips to position my form. It works well enough and if I can't find any other solutions, it will have to do.. but I'd really like to position my window just below the button that's being clicked on.

If you click on the button, you get a GetFocus event. Inside the event you call GetCursorPos. Wouldn't that suffice to position the window? If it's a toolbar, it will glue to the top, left, bottom or right of the parent and you know the parents position. And you would be able to calculate your button position with other buttons in the toolbar? If they have same width it would be even easier?


Hrmm.. Well if I use GetCursorPos, I can determine where the mouse is when the user clicked on the button, but I still can't know where he clicked inside that button, so I still have no way of finding the bottom left corner of the button the user clicked on.

I think you can if you know the position of the toolbars parent window?


The toolbar is in one activeX project, and its parent is a second activex project.. and as I stated above, activex1.parent.top generates the following error message:
"Client Site Not Available"

I apreciate your help kimpan!

How about using GetWindowRect?

If you call it with your toolbar.hWnd you would get it's relative position to screen. In GetFocus you get which button and button.left is relative to the toolbar or usercontrol.. wouldn't that suffice?



Wait.. you may be on to something. How does GetWindowRect work? I'm rather new to the API. But if I can use an API call to get the the toolbar's upper left coordinates relative to the screen, then I simply need to add Button.Left and Button.Top + Button.Height to those coordinates!
Author of the Year 2009

Hi DocNsanE,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Kimpan's comment(s) as an answer.

DocNsanE, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial