Redigera

Dela via


CommonDialog.ShowDialog Method

Definition

Runs a common dialog box.

Overloads

ShowDialog()

Runs a common dialog box with a default owner.

ShowDialog(IWin32Window)

Runs a common dialog box with the specified owner.

ShowDialog()

Runs a common dialog box with a default owner.

public:
 System::Windows::Forms::DialogResult ShowDialog();
public System.Windows.Forms.DialogResult ShowDialog ();
member this.ShowDialog : unit -> System.Windows.Forms.DialogResult
Public Function ShowDialog () As DialogResult

Returns

OK if the user clicks OK in the dialog box; otherwise, Cancel.

Examples

The following code example uses the ColorDialog implementation of CommonDialog and illustrates creating and showing a dialog box. This example requires that the method is called from within an existing form, which has a TextBox and Button placed on it.

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      ColorDialog^ MyDialog = gcnew ColorDialog;
      // Keeps the user from selecting a custom color.
      MyDialog->AllowFullOpen = false;
      // Allows the user to get help. (The default is false.)
      MyDialog->ShowHelp = true;
      // Sets the initial color select to the current text color.
      MyDialog->Color = textBox1->ForeColor;
      
      // Update the text box color if the user clicks OK 
      if ( MyDialog->ShowDialog() == ::System::Windows::Forms::DialogResult::OK )
      {
         textBox1->ForeColor = MyDialog->Color;
      }
   }
private void button1_Click(object sender, System.EventArgs e)
 {
    ColorDialog MyDialog = new ColorDialog();
    // Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = false ;
    // Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = true ;
    // Sets the initial color select to the current text color.
    MyDialog.Color = textBox1.ForeColor ;
    
    // Update the text box color if the user clicks OK 
    if (MyDialog.ShowDialog() == DialogResult.OK)
        textBox1.ForeColor =  MyDialog.Color;
 }
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim MyDialog As New ColorDialog()
    ' Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = False
    ' Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = True
    ' Sets the initial color select to the current text color,
    MyDialog.Color = TextBox1.ForeColor

    ' Update the text box color if the user clicks OK 
    If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        TextBox1.ForeColor = MyDialog.Color
    End If
End Sub

Remarks

This method implements RunDialog.

See also

Applies to

ShowDialog(IWin32Window)

Runs a common dialog box with the specified owner.

public:
 System::Windows::Forms::DialogResult ShowDialog(System::Windows::Forms::IWin32Window ^ owner);
public System.Windows.Forms.DialogResult ShowDialog (System.Windows.Forms.IWin32Window owner);
public System.Windows.Forms.DialogResult ShowDialog (System.Windows.Forms.IWin32Window? owner);
member this.ShowDialog : System.Windows.Forms.IWin32Window -> System.Windows.Forms.DialogResult
Public Function ShowDialog (owner As IWin32Window) As DialogResult

Parameters

owner
IWin32Window

Any object that implements IWin32Window that represents the top-level window that will own the modal dialog box.

Returns

OK if the user clicks OK in the dialog box; otherwise, Cancel.

Remarks

This version of the ShowDialog method allows you to specify a specific form or control that will own the dialog box that is shown. If you use the version of this method that has no parameters, the dialog box being shown would be owned automatically by the currently active window of your application.

Applies to