4

I want to create a modal popup. So far I have made a window which I create when I need it. But I don't think that is the right way to do this. The problem is, everytime I call it, it opens about 20px to the right and 20px lower than previous one. It's annoying. Is this default behaviour or am I doing something wrong here?

Success win1 = new Success();
win1.ShowDialog();

Also, I want it to be centered if that's possible?

animuson
  • 53,861
  • 28
  • 137
  • 147
  • 4
    This is actually by design, and I don't see how it would annoy. This is covered by the Windows UX Guidelines: http://msdn.microsoft.com/en-us/library/aa511262.aspx – BoltClock May 29 '11 at 23:20

2 Answers2

6

Below will center the your dialog box to the owner/parent form.

Success win1 = new Success();
win1.Owner = this; // For example , see the parent window here
win1.WindowStartupLocation = WindowStartupLocation.CenterOwner;
win1.ShowDialog();

Below are the alternative if you want to try. If you want to Center it to the screen use 'CenterScreen'

Manual - The startup location of a Window is set from code, or defers to the default Windows location.

CenterScreen - The startup location of a Window is the center of the screen that contains the mouse cursor.

CenterOwner - The startup location of a Window is the center of the Window that owns it, as specified by the Window.Owner property.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
4

You want to set the WindowStartupLocation member of the Success window. Sounds like CenterOwner is the value you want.

cristobalito
  • 4,192
  • 1
  • 29
  • 41
  • 1
    I would note that you also have to set the Owner property in order for that to work. – Ed S. May 29 '11 at 23:23
  • I think I must be doing something wrong as this just centered to the whole screen, not my app.. Thanks anyway! –  May 29 '11 at 23:56