Tgys
Tgys

Reputation: 622

How to disable a form when a popup-ish form is being shown?

I have a "main" form (form1). Within that form I successfully open another form (form2) - but - when that form is open, you can still control the underlying form (form1).

I don't want the user to be able to do so, and whenever he tries to click on it or something, the popped-up form (form2) should gain focus/flash a bit, with some default Windows sound. I've just described how it acts if the popup window is a FileSave/OpenDialog. Those dialogs work exactly as I want them to work.

I've tried setting form2.Owner = form1; but that did not result in the desired effect.

So my question basically is: how can I get the same effect of focus/etc. on a form - just how it is on a Save/OpenDialog?

Thanks,

~ Tgys

Upvotes: 1

Views: 2572

Answers (1)

ABH
ABH

Reputation: 3439

To open the form2 use form2.ShowDialog()

In form1 class

form2 form2Object = new form2();
form2.ShowDialog(this);

This way form1 will remain in the background and un-clickable until form2 is shown. You can also return the dialog result from form2 if you want. Code from MSDN.

  Form2 testDialog = new Form2();

   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if (testDialog.ShowDialog(this) == DialogResult.OK)
   {
      // Read the contents of testDialog's TextBox.
      this.txtResult.Text = testDialog.TextBox1.Text;
   }
   else
   {
      this.txtResult.Text = "Cancelled";
   }

Upvotes: 6

Related Questions