2

I have an application like this:

enter image description here

The yellow form will appear when user click to a button on the main Form and i want that yellow form appear at the position where the cursor is pointing to. (in the picture is button number 26). But it just appears somewhere else (just like picture too)

Here is my onClick event of button on the main form:

ChooseAnswer Answer = new ChooseAnswer();
Answer.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
Answer.ShowDialog();

What's wrong with this?

Sorry about my english!

OK the solution is:

ChooseAnswer Answer = new ChooseAnswer();
Answer.StartPosition = FormStartPosition.Manual;
Answer.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
Answer.ShowDialog();
tuankhoa1996
  • 131
  • 4
  • 18
  • Where is this code located, inside a click handler for these buttons? Or you want this form to move around with your mouse, like a tooltip? – vgru Dec 08 '15 at 08:06
  • no. just appear when i click the button with number in it (in the main form) – tuankhoa1996 Dec 08 '15 at 08:06
  • I think the cursor position you get is relative to the window, not to the screen. – wimh Dec 08 '15 at 08:09
  • any idea to fix that? :( – tuankhoa1996 Dec 08 '15 at 08:10
  • 1
    @Wimmel: `Cursor.Position` returns screen coordinates. But the problem is that `ShowDialog` uses `Form.StartPosition` to determine its initial position, and it has to be set to `Manual`, like [described in this answer](http://stackoverflow.com/questions/998675/how-can-i-control-the-location-of-a-dialog-when-using-showdialog-to-display-it). – vgru Dec 08 '15 at 08:12
  • 1
    I have found the solution! But still thank you! :) – tuankhoa1996 Dec 08 '15 at 08:14
  • 1
    Tuan, please do share what you changed to fix so it will help others who are looking for the same answer. Thanks, – NPToita Dec 08 '15 at 08:14
  • @tuankhoa1996: Note also that you don't need to create a new `Point` struct, simply set `Form.StartPosition = FormStartPosition.Manual;` and `Form.Location = Cursor.Position;` and you're fine. – vgru Dec 08 '15 at 08:17
  • oh! good point. thank you so much @Groo! :) – tuankhoa1996 Dec 09 '15 at 00:09

1 Answers1

0

Try this;

ChooseAnswer Answer = new ChooseAnswer();
Answer.Top = Cursor.Position.Y;
Answer.Left = Cursor.Position.X;
Answer.ShowDialog();
Irshad
  • 3,071
  • 5
  • 30
  • 51