CommonDialog.ShowDialog 方法

定義

執行通用對話方塊。

多載

ShowDialog()

以預設的擁有人來執行通用對話方塊。

ShowDialog(IWin32Window)

以指定的擁有人來執行通用對話方塊。

ShowDialog()

以預設的擁有人來執行通用對話方塊。

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

傳回

如果使用者在對話方塊中按一下 [確定],則為 OK,否則為 Cancel

範例

下列程式碼範例會 ColorDialog 使用 的 實作 CommonDialog ,並說明如何建立及顯示對話方塊。 這個範例會要求從現有表單內呼叫 方法,該表單上具有 TextBoxButton 置於其中。

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

備註

這個方法會實作 RunDialog

另請參閱

適用於

ShowDialog(IWin32Window)

以指定的擁有人來執行通用對話方塊。

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

參數

owner
IWin32Window

實作 IWin32Window 的任何物件,代表將擁有強制回應對話方塊的最上層視窗。

傳回

若使用者在對話方塊中按一下 [確定],則為 OK;否則為 Cancel

備註

這個版本的 ShowDialog 方法可讓您指定將擁有所顯示對話方塊的特定表單或控制項。 如果您使用沒有參數的這個方法版本,則應用程式的目前使用中視窗會自動擁有所顯示的對話方塊。

適用於