Hello, Im setting up some buttons that open a UserForm, is there any way of making it open them next to the mouse cursor?
Thanks
Open a Userform next to mouse cursor
- richardcranney
- Closed
Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.
-
-
-
Re: Open a Userform next to mouse cursor
Richard,
AFAIK positioning forms relative to a cell address is really tough and probably best avoided. See
http://www.cpearson.com/excel/FormPosition.htm
I do not know of a way to get the cursor position but you could get the activecell with ActiveCell.Address. I guess you could use sendkeys to force an active cell.
Carl -
Re: Open a Userform next to mouse cursor
Hi, thanks.
I've seen it done before, it was used to replicate a menu structure when clicking a button, it would open the list next to the button. It would be good to know it its possible to pull the mouse coordinates from somewhere to replicate this, any help again would be appriciated.
-
Re: Open a Userform next to mouse cursor
Richard
Is what you are looking for perhaps a custom commandbar that is opened when a cell is right clicked?
-
-
-
Re: Open a Userform next to mouse cursor
Its just really to have a custom menu appear.
when the user clicks on the button 'Accounts' at the top of the spreadsheet, either a menu, or userform as roy suggested appears next to the mousepointer.
Im whizzing through some google searches at the minute and putting 4 or 5 pieces of code together to see if i can get the deisred effect, but any help would be appriciated.
Thanks
-
Re: Open a Userform next to mouse cursor
Hi Roy, Norrie,
Ive concuted this, The only problem is it seems to open the userform a bit offset to the mouse pointer and it changes where it opens depending on where abouts in the button you click.
Any ideas?
-
Re: Open a Userform next to mouse cursor
have you checked Chip Pearson's Form Positioner as suggested earlier?
-
-
Re: Open a Userform next to mouse cursor
I've also been looking at creating menu's. Using the ozgrid guide, I can now create menus such as the file, edit, view, favourites, tools and help.
Is there a way of recreating the Right mouse button menu when left clicking on the object, but with my custom menu in there?
-
-
Re: Open a Userform next to mouse cursor
Richard
Perhaps you could take a look at the worksheet event BeforeRightClick?
-
Re: Open a Userform next to mouse cursor
Quote from norieRichard
Perhaps you could take a look at the worksheet event BeforeRightClick?
Thanks Norrie,
The beforerightclick lets you do some code before the actual menu appears doesnt it?
Im actually wanting to create a new menu, just cant favem out how to create one like the one when you rightclick.
-
-
Here's an idea. All the methods of open a userform near the cursor seem to rely on Windows API calls, but what about if you just use the MouseDown function. At least if you're opening a second userform from another, then the MouseDown event of the object on the userform, you're going to click, dlb-click etc. to open the 2nd form provides the X and Y coords within that object. So to position the 2nd userform, in the project global variables (say Module 1), define:
Public CursorX, CursorY as Single
WIthin the MouseDown event of the object include:
CursorX = X
CursorY = Y
Within the Userform2_Initialise include
Userform2.left = Userform1.left + Userform2.obj.left + CursorX
Userform2.Top = Userform2.top + Userform2.obj.top + CursorY + 25
Then userform2 will appear to the right of the cursor position. The 25 offset in Userform2.Top is to account for the title bar.
Where possible I avoid Windows APIs as they are not portable to, say, Mac OSX and if you have them in your code, you need to have conditional statements checking for the OS to be Windows or not - messy if it can be avoided.
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!