LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio / WPF > 22 多窗口作业练习说明 > .DialogResult | error:Window_Closed()におけるDialogResultの代入

Last updated at Posted at 2017-06-14
Visual Studio 2017 Community (以下VS)
Windows 7 Pro (32bit)

10:28頃

win.ShowDialog();の戻り値ではなく、
win.Result;を後から取得している。

実際には.Resultではなく、.DialogResultというプロパティを取得するようだ。

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170526_t1910_winResult
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            InputWindow inpWin = new InputWindow();
            inpWin.ShowDialog();
            Nullable<bool> res = inpWin.DialogResult;
            if (res == null)
            {
                MessageBox.Show("null");
            } else if (res == true)
            {
                MessageBox.Show("true");
            } else
            {
                MessageBox.Show("false");
            }
        }
    }
}
InputWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace _170526_t1910_winResult
{
    /// <summary>
    /// InputWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class InputWindow : Window
    {
        public InputWindow()
        {
            InitializeComponent();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            // DialogResult = true;
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
    }
}

補足

Window_Closed()においてDialogResultに値を代入しようとするとエラーが出た。

System.InvalidOperationException: 'Window を作成し、ダイアログとして表示した後でのみ DialogResult を設定できます。'

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0