Window.DialogResult プロパティ

定義

ダイアログ ボックスの結果値を取得または設定します。この値は、ShowDialog() メソッドから返される値です。

public:
 property Nullable<bool> DialogResult { Nullable<bool> get(); void set(Nullable<bool> value); };
[System.ComponentModel.TypeConverter(typeof(System.Windows.DialogResultConverter))]
public bool? DialogResult { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Windows.DialogResultConverter))>]
member this.DialogResult : Nullable<bool> with get, set
Public Property DialogResult As Nullable(Of Boolean)

プロパティ値

Nullable<T> 型の Boolean 値。 既定値は、false です。

属性

例外

DialogResult を呼び出すことによってウィンドウが開かれる前に ShowDialog() が設定されます。

- または -

DialogResult を呼び出すことによって開かれたウィンドウに Show() が設定されます。

次の例では、適切な DialogResultを返すように [OK] ボタンと [キャンセル] ボタンを構成する方法を示します。

<Button IsDefault="True" Click="acceptButton_Click">OK (IsDefault=True)</Button>
<Button IsCancel="True">Cancel (IsCancel=True)</Button>
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) 判断するために使用できます。 ダイアログ ボックスが受け入れられた場合は、ユーザーによって収集されたデータを取得して処理するためにダイアログ ボックスを開いたコードが示されます。 ただし、ダイアログ ボックスが取り消された場合は、呼び出し元のコードがそれ以降の処理を停止する必要があることを示します。

既定では、ユーザーが次のいずれかの操作を行うと、ダイアログ ボックスが取り消されます。

  • PressesALT + F4。

  • [ 閉じる ] ボタンをクリックします。

  • [システム] メニューから [ 閉じる ] を選択します。

これらのいずれの場合も、 DialogResult は既定で です false

通常、ダイアログ ボックスには、ダイアログを取り消す特別なボタンが用意されています。これは、 プロパティが にtrue設定されているボタンIsCancelです。 この方法で構成されたボタンは、ウィンドウが押されたとき、または ESC キーが押されたときに自動的にウィンドウを閉じます。 いずれの場合も、 DialogResult は のままです false

ダイアログ ボックスには通常、受け入れボタンも用意されています。これは、 プロパティが にtrue設定されているボタンIsDefaultです。 この方法で構成されたボタンは、Enter キーまたは Enter キーが押されたときにイベントを発生 Click させます。 ただし、ダイアログ ボックスは自動的に閉じられません。また、 は にtrue設定DialogResultされません。 このコードは、通常は既定のボタンの Click イベント ハンドラーから手動で記述する必要があります。

DialogResultnull 、ダイアログ ボックスが表示されていても、受け入れも取り消しもしない場合です。

ダイアログ ボックスが閉じると、メソッドによって返された値から、または プロパティを調べることによって ShowDialog ダイアログ結果を DialogResult 取得できます。

DialogResult は、 メソッドを Window 呼び出 ShowDialog して を開いた場合にのみ設定できます。

注意

ウィンドウがブラウザーでホストされている場合、このプロパティを設定または取得することはできません。

適用対象