Window.DialogResult 属性 (System.Windows)

Window.DialogResult 属性 (System.Windows)

获取或设置对话框结果值,该值是从 ShowDialog() 方法返回的值。

public:

property Nullable DialogResult { Nullable get(); void set(Nullable value); };

[System.ComponentModel.TypeConverter(typeof(System.Windows.DialogResultConverter))]

public bool? DialogResult { get; set; }

[]

member this.DialogResult : Nullable with get, set

Public Property DialogResult As Nullable(Of Boolean)

属性值

Nullable

Boolean类型的 Nullable 值。 默认值为 false。

属性

TypeConverterAttribute

例外

InvalidOperationException

通过调用 ShowDialog()在打开窗口之前设置 DialogResult。

-或-

DialogResult 在通过调用 Show()打开的窗口上设置。

示例

以下示例演示如何配置“确定”按钮和“取消”按钮以返回相应的 DialogResult。

using System;

using System.Windows;

using System.Windows.Controls;

namespace CSharp

{

public partial class DialogBox : Window

{

public DialogBox()

{

InitializeComponent();

}

// The accept button is a button whose IsDefault property is set to true.

// This event is raised whenever this button is clicked, or the ENTER key

// is pressed.

void acceptButton_Click(object sender, RoutedEventArgs e)

{

// Accept the dialog and return the dialog result

this.DialogResult = true;

}

}

}

Imports System.Windows

Imports System.Windows.Controls

Namespace VisualBasic

Partial Public Class DialogBox

Inherits Window

Public Sub New()

InitializeComponent()

End Sub

' The accept button is a button whose IsDefault property is set to true.

' This event is raised whenever this button is clicked, or the ENTER key

' is pressed.

Private Sub acceptButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

' Accept the dialog and return the dialog result

Me.DialogResult = True

End Sub

End Class

End Namespace

注解

DialogResult 可用于显示对话框的代码,以确定用户是接受(true)还是取消(false)对话框。 如果接受对话框,则表示打开对话框的代码以检索用户收集的数据并处理它。 但是,如果取消了对话框,则表示调用代码应停止任何进一步处理。

默认情况下,当用户执行以下操作之一时,将取消对话框:

按 Alt+F4。

单击“关闭”按钮 。

从“系统”菜单中选择 “关闭”。

在所有这些情况下,默认情况下 falseDialogResult。

对话框通常提供一个用于取消对话框的特殊按钮,即 IsCancel 属性设置为 true的按钮。 这样配置的按钮会在按下窗口或按下 ESC 键时自动关闭窗口。 在这两种情况下,DialogResult 仍然 false。

对话框通常还提供接受按钮,即 IsDefault 属性设置为 true的按钮。 按这种方式配置的按钮会在按下或按 Enter 键时引发其 Click 事件。 但是,它不会自动关闭对话框,也不会将 DialogResult 设置为 true。 需要手动编写此代码,通常是默认按钮的 Click 事件处理程序。

当显示对话框但未接受或取消时,DialogResultnull。

对话框关闭后,可以从 ShowDialog 方法返回的值或检查 DialogResult 属性来获取对话框结果。

仅当通过调用 ShowDialog 方法打开 Window 时,才能设置 DialogResult。

注意

在浏览器中托管窗口时,无法设置或获取此属性。

适用于

相关推荐

怎么回复朋友的祝福语汇总
h365邮箱官网

怎么回复朋友的祝福语汇总

2025-09-04 👁️ 8315
2025年全国各省艺术统考成绩查询时间汇总(附统考成绩查询通道)
银行征信黑名单的欠款逾期记录多久会解除,刷新时间是几年?
简约好看实用的浏览器主页推荐
h365邮箱官网

简约好看实用的浏览器主页推荐

2025-09-28 👁️ 2392
PH是哪个国家
日博365bet手机版

PH是哪个国家

2025-07-15 👁️ 9255
大都会杂志
h365邮箱官网

大都会杂志

2025-09-09 👁️ 8271