Showing a window right under the cursor

J

Jonathan Sion

Hi,
I dont think this one is too dificult: i wish to show a windows form ,
its upper left corner would be right where the mouse is right now,
(sort of like a popup menu)

what i do is is something like:

dim loc as Point = Cursor.Position
myForm..SetDesktopLocation(loc .X, loc .Y)
myForm.Show

however, the window is never appearing where i want it to... what's
wrong?

thanks
 
S

ShaneO

Jonathan said:
Hi,
I dont think this one is too dificult: i wish to show a windows form ,
its upper left corner would be right where the mouse is right now,
(sort of like a popup menu)

what i do is is something like:

dim loc as Point = Cursor.Position
myForm..SetDesktopLocation(loc .X, loc .Y)
myForm.Show

however, the window is never appearing where i want it to... what's
wrong?

thanks

Your myForm.Show is shown relative to the Desktop, whereas your
Cursor.Position is relative to the current Form.

Add the X,Y position of the current Form to the
myForm.SetDesktopLocation(loc.X, loc.Y) and you'll find your myForm.Show
will open in the correct position.


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
J

Jonathan Sion

Thanks Shane
while I believe you are right, this still doesnt work.. and I am not
sure what's the reason. somehow the form doesnt take the coordinates i
give it. the form has a method, something like this:

Public Sub ShowThisForm(ByVal atPos As Point)
Me.SetDesktopLocation(atPos.X, atPos.Y)
Me.TopLevel = True
Me.Show()
End Sub

and it keeps showing all over the place... each time at a different
position. would you know why?

thanks again
 
N

Nyx37

how are you instangating the form, and are you seting the the StartPosition
property?
 
N

Nyx37

To further expand on my reply.
I would instagate the form and set the properties before calling the show
method.
an example: (pls excuse any mistakes as I am not where I can access VS).

Dim frm as FormToShow = New FormToShow

frm.StartPosition = FormStartPosition.Manual
frm.TopLevel = True
frm.Location = New Point(Cursor.Position.X + ActiveForm.Location.X, _
Cursor.Position.Y + ActiveForm.Location.Y)
frm.Show()
 

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