24

I have a Windows form from which I would like to open a status form that says "Saving..." and then disapears when the saving is complete. I would like to center this small status form in the middle of the calling form. I've tried setting the "StartPosition" propery to "CenterParent", but it doest work. I create the status form from the other form like so:

SavingForm saving = new SavingForm();
savingForm.Show();
Thread.Sleep(500); //Someone said this is bad practice ... why?
savingForm.Close();

Wouldn't the calling form be the "Parent"? When I set a watch for saving it says it has no parent.

I tried:

SavingForm saving = new SavingForm();
saving.Parent = this;
savingForm.Show();
Thread.Sleep(500);
savingForm.Close();

and it throws an exception "Top-level control cannot be added to a control."

How do I center this status window in the calling window?

Thanks in advance

Matt
  • 25,467
  • 18
  • 120
  • 187
Greycrow
  • 1,603
  • 5
  • 19
  • 29
  • 1
    The Show.. Sleep.. Close.. code is not guarenteed to actually show anything. Also you're UI thread hangs during the Sleep so it might introduce UI glitches. – CodingBarfield Oct 30 '12 at 09:00
  • 1
    Very informative related post which is at the root of this question - [Whats the difference between Parentform and Owner](https://stackoverflow.com/q/28377212/465053) – RBT Apr 05 '18 at 09:07

3 Answers3

21

i would do something like this:

SavingForm saving = new SavingForm();
savingForm.ShowDialog(this);

In SavingForm i would start a timer in the load handler that runs for 500 milliseconds and then closes the form when done. Cleaner that way. ShowDialog will also lock your UI to only display the saving form and not allow the user to monkey with anything.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • There doesn't seem to be a `Form` ctor that takes an owner? http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx – Blorgbeard Jun 19 '13 at 07:01
  • @Blorgbeard: Good eyes. I put `this` into the wrong method (wrote this up without an IDE/compiler handy.) See my edit. I meant `savingForm.ShowDialog(this);` – Paul Sasik Jun 19 '13 at 22:17
18

Use this:

saving.Show(this);

To set the Owner when you show the form.

Edit: The ShowDialog() method also has an overload that let's you specify the owner if that is the route you decide to go:

saving.ShowDialog(this);
Cory Charlton
  • 8,868
  • 4
  • 48
  • 68
6

If you pass the parent (this) to the Owner, like

SavingForm saving = new SavingForm() { Owner = this };

then you can access Owner's properties and methods in the child form (in this case SavingForm), provided that the Owner's properties Modifier is set to Internal or Public for each property you need to access (you can either edit the modifier directly in the source code, or via form's designer properties - there is a Modifier property for each control).

You can find a nice explanation of the differences between Owner, Parent and ParentForm here.

Note: Passing it like saving.Show(this); or saving.ShowDialog(this); did not help in my case.

Matt
  • 25,467
  • 18
  • 120
  • 187