Form.DialogResult 属性

获取或设置窗体的对话框结果。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property DialogResult As DialogResult
用法
Dim instance As Form
Dim value As DialogResult

value = instance.DialogResult

instance.DialogResult = value
public DialogResult DialogResult { get; set; }
public:
property DialogResult DialogResult {
    DialogResult get ();
    void set (DialogResult value);
}
/** @property */
public DialogResult get_DialogResult ()

/** @property */
public void set_DialogResult (DialogResult value)
public function get DialogResult () : DialogResult

public function set DialogResult (value : DialogResult)

属性值

DialogResult,表示当窗体用作对话框时该窗体的结果。

异常

异常类型 条件

InvalidEnumArgumentException

指定值不在有效值范围内。

备注

窗体的对话框结果是当窗体显示为模式对话框时从该窗体返回的值。如果窗体显示为对话框,用 DialogResult 枚举中的值设置此属性将设置该窗体的对话框结果值、隐藏模式对话框并将控制返回给调用窗体。此属性通常由窗体上 Button 控件的 DialogResult 属性设置。当用户单击 Button 控件时,分配给 ButtonDialogResult 属性的值将分配给该窗体的 DialogResult 属性。

当窗体显示为模式对话框时,单击“关闭”按钮(窗体右上角带 X 的按钮)会隐藏窗体并将 DialogResult 属性设置为 DialogResult.Cancel。当用户单击对话框的“关闭”按钮或设置 DialogResult 属性的值时,不会自动调用 Close 方法。而是隐藏该窗体并可重新显示该窗体,而不用创建该对话框的新实例。因为此行为,所以当应用程序不再需要该窗体时,必须调用该窗体的 Dispose 方法。

可以使用此属性确定对话框是如何关闭的,以便正确处理在该对话框中执行的操作。

提示

通过在窗体的 Closing 事件的事件处理程序中设置 DialogResult 属性,可以重写用户单击“关闭”按钮时分配给 DialogResult 属性的值。

提示

如果 Form 显示为无模式窗口,则 DialogResult 属性返回的值可能不返回分配给该窗体的值,因为关闭该窗体时将自动释放该窗体的资源。

示例

下面的代码示例将窗体显示为对话框并显示一个消息框,该消息框通过引用窗体的 DialogResult 属性,指示单击了窗体的“确定”还是“取消”按钮。

Public Sub CreateMyForm()
    ' Create a new instance of the form.
    Dim form1 As New Form()
    ' Create two buttons to use as the accept and cancel buttons.
    Dim button1 As New Button()
    Dim button2 As New Button()
    
    ' Set the text of button1 to "OK".
    button1.Text = "OK"
    ' Set the position of the button on the form.
    button1.Location = New Point(10, 10)
    ' Set the text of button2 to "Cancel".
    button2.Text = "Cancel"
    ' Set the position of the button based on the location of button1.
    button2.Location = New Point(button1.Left, button1.Height + button1.Top + 10)
    ' Make button1's dialog result OK.
    button1.DialogResult = DialogResult.OK
    ' Make button2's dialog result Cancel.
    button2.DialogResult = DialogResult.Cancel
    ' Set the caption bar text of the form.   
    form1.Text = "My Dialog Box"
    
    ' Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog
    ' Set the accept button of the form to button1.
    form1.AcceptButton = button1
    ' Set the cancel button of the form to button2.
    form1.CancelButton = button2
    ' Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen
    
    ' Add button1 to the form.
    form1.Controls.Add(button1)
    ' Add button2 to the form.
    form1.Controls.Add(button2)
    
    ' Display the form as a modal dialog box.
    form1.ShowDialog()
    
    ' Determine if the OK button was clicked on the dialog box.
    If form1.DialogResult = DialogResult.OK Then
        ' Display a message box indicating that the OK button was clicked.
        MessageBox.Show("The OK button on the form was clicked.")
        ' Optional: Call the Dispose method when you are finished with the dialog box.
        form1.Dispose
    ' Display a message box indicating that the Cancel button was clicked.
    Else
        MessageBox.Show("The Cancel button on the form was clicked.")
        ' Optional: Call the Dispose method when you are finished with the dialog box.
        form1.Dispose
    End If
End Sub 'CreateMyForm 
public void CreateMyForm()
 {
    // Create a new instance of the form.
    Form form1 = new Form();
    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button ();
    Button button2 = new Button ();
   
    // Set the text of button1 to "OK".
    button1.Text = "OK";
    // Set the position of the button on the form.
    button1.Location = new Point (10, 10);
    // Set the text of button2 to "Cancel".
    button2.Text = "Cancel";
    // Set the position of the button based on the location of button1.
    button2.Location 
       = new Point (button1.Left, button1.Height + button1.Top + 10);
    // Make button1's dialog result OK.
    button1.DialogResult = DialogResult.OK;
    // Make button2's dialog result Cancel.
    button2.DialogResult = DialogResult.Cancel;
    // Set the caption bar text of the form.   
    form1.Text = "My Dialog Box";
 
    // Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog;
    // Set the accept button of the form to button1.
    form1.AcceptButton = button1;
    // Set the cancel button of the form to button2.
    form1.CancelButton = button2;
    // Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen;
    
    // Add button1 to the form.
    form1.Controls.Add(button1);
    // Add button2 to the form.
    form1.Controls.Add(button2);
    
    // Display the form as a modal dialog box.
    form1.ShowDialog();
 
    // Determine if the OK button was clicked on the dialog box.
    if (form1.DialogResult == DialogResult.OK)
    {
       // Display a message box indicating that the OK button was clicked.
       MessageBox.Show("The OK button on the form was clicked.");
       // Optional: Call the Dispose method when you are finished with the dialog box.
       form1.Dispose();
    }
    else
    {
       // Display a message box indicating that the Cancel button was clicked.
       MessageBox.Show("The Cancel button on the form was clicked.");
       // Optional: Call the Dispose method when you are finished with the dialog box.
       form1.Dispose();
    }
 }
    
void CreateMyForm()
{
   
   // Create a new instance of the form.
   Form^ form1 = gcnew Form;
   
   // Create two buttons to use as the accept and cancel buttons.
   Button^ button1 = gcnew Button;
   Button^ button2 = gcnew Button;
   
   // Set the text of button1 to "OK".
   button1->Text = "OK";
   
   // Set the position of the button on the form.
   button1->Location = Point(10,10);
   
   // Set the text of button2 to "Cancel".
   button2->Text = "Cancel";
   
   // Set the position of the button based on the location of button1.
   button2->Location = Point(button1->Left,button1->Height + button1->Top + 10);
   
   // Make button1's dialog result OK.
   button1->DialogResult = ::DialogResult::OK;
   
   // Make button2's dialog result Cancel.
   button2->DialogResult = ::DialogResult::Cancel;
   
   // Set the caption bar text of the form.   
   form1->Text = "My Dialog Box";
   
   // Define the border style of the form to a dialog box.
   form1->FormBorderStyle = ::FormBorderStyle::FixedDialog;
   
   // Set the accept button of the form to button1.
   form1->AcceptButton = button1;
   
   // Set the cancel button of the form to button2.
   form1->CancelButton = button2;
   
   // Set the start position of the form to the center of the screen.
   form1->StartPosition = FormStartPosition::CenterScreen;
   
   // Add button1 to the form.
   form1->Controls->Add( button1 );
   
   // Add button2 to the form.
   form1->Controls->Add( button2 );
   
   // Display the form as a modal dialog box.
   form1->ShowDialog();
   
   // Determine if the OK button was clicked on the dialog box.
   if ( form1->DialogResult == ::DialogResult::OK )
   {
      
      // Display a message box indicating that the OK button was clicked.
      MessageBox::Show( "The OK button on the form was clicked." );
      
      // Optional: Call the Dispose method when you are finished with the dialog box.
      delete form1;
   }
   else
   {
      
      // Display a message box indicating that the Cancel button was clicked.
      MessageBox::Show( "The Cancel button on the form was clicked." );
      
      // Optional: Call the Dispose method when you are finished with the dialog box.
      delete form1;
   }
}
public void CreateMyForm()
{
    // Create a new instance of the form.
    Form form1 = new Form();

    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button();
    Button button2 = new Button();

    // Set the text of button1 to "OK".
    button1.set_Text("OK");

    // Set the position of the button on the form.
    button1.set_Location(new Point(10, 10));

    // Set the text of button2 to "Cancel".
    button2.set_Text("Cancel");

    // Set the position of the button based on the location of button1.
    button2.set_Location(new Point(button1.get_Left(), 
        button1.get_Height() + button1.get_Top() + 10));

    // Make button1's dialog result OK.
    button1.set_DialogResult(get_DialogResult().OK);

    // Make button2's dialog result Cancel.
    button2.set_DialogResult(get_DialogResult().Cancel);

    // Set the caption bar text of the form.   
    form1.set_Text("My Dialog Box");

    // Define the border style of the form to a dialog box.
    form1.set_FormBorderStyle(get_FormBorderStyle().FixedDialog);

    // Set the accept button of the form to button1.
    form1.set_AcceptButton(button1);

    // Set the cancel button of the form to button2.
    form1.set_CancelButton(button2);

    // Set the start position of the form to the center of the screen.
    form1.set_StartPosition(FormStartPosition.CenterScreen);

    // Add button1 to the form.
    form1.get_Controls().Add(button1);

    // Add button2 to the form.
    form1.get_Controls().Add(button2);

    // Display the form as a modal dialog box.
    form1.ShowDialog();

    // Determine if the OK button was clicked on the dialog box.
    if (form1.get_DialogResult().Equals(get_DialogResult().OK)) {
        // Display a message box indicating that the OK button was clicked.
        MessageBox.Show("The OK button on the form was clicked.");

        // Optional: Call the Dispose method when you are finished with 
        // the dialog box.
        form1.Dispose();
    }
    else {
        // Display a message box indicating that the Cancel button was 
        // clicked.
        MessageBox.Show("The Cancel button on the form was clicked.");

        // Optional: Call the Dispose method when you are finished with 
        // the dialog box.
        form1.Dispose();
    }
} //CreateMyForm

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Form 类
Form 成员
System.Windows.Forms 命名空间
DialogResult 枚举
Button.DialogResult 属性