Export (0) Print
Expand All

Form.ShowDialog Method

Shows the form as a modal dialog box.

Overload List

Shows the form as a modal dialog box with no owner window.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ShowDialog() As DialogResult
[C#] public DialogResult ShowDialog();
[C++] public: DialogResult ShowDialog();
[JScript] public function ShowDialog() : DialogResult;

Shows the form as a modal dialog with the specified owner.

[Visual Basic] Overloads Public Function ShowDialog(IWin32Window) As DialogResult
[C#] public DialogResult ShowDialog(IWin32Window);
[C++] public: DialogResult ShowDialog(IWin32Window*);
[JScript] public function ShowDialog(IWin32Window) : DialogResult;

Example

[Visual Basic, C#, C++] The following example displays a form as a modal dialog box and evaluates the return value of the dialog box before determining whether to read the value of a TextBox control on the dialog box form. This example assumes that a Form named Form2 is created and that it contains a TextBox control named TextBox1. The example uses the version of ShowDialog that specifies an owner for the dialog box.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ShowDialog. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub ShowMyDialogBox()
    Dim testDialog As New Form2()
    
    ' Show testDialog as a modal dialog and determine if DialogResult = OK.
    If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
        ' Read the contents of testDialog's TextBox.
        txtResult.Text = testDialog.TextBox1.Text
    Else
        txtResult.Text = "Cancelled"
    End If
    testDialog.Dispose()
End Sub 'ShowMyDialogBox

[C#] 
public void ShowMyDialogBox()
{
   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";
   }
   testDialog.Dispose();
}

[C++] 
public:
   void ShowMyDialogBox()
   {
      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 = S"Cancelled";
      }
      testDialog->Dispose();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Form Class | Form Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft