Showing a window right under the cursor

  • Thread starter Thread starter Jonathan Sion
  • Start date Start date
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
 
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.
 
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
 
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()
 
Back
Top