简体   繁体   中英

MessageBox.Show Dialog Result / C# .NET 2017 Community

I want to make a question box (MessageBox.Show) but I can't figure out how to get the Dialog Results. I looked at every posts, videos, but I can't seem to fine for the 2017 .NET (Wpf) version!

(Basically I want to make a question after InitializeComponents() )

Here is my code.

(代码在这里)

WPF

If you're using WPF MessageBox.Show() does return a MessageBoxResult :

MessageBoxResult result = MessageBox.Show("asd", "xcvxcv", MessageBoxButton.OKCancel);

if (result == MessageBoxResult.OK)
    MessageBox.Show("Ok was selected");

Windows Forms

If you're using Windows Forms MessageBox.Show() does return a DialogResult :

DialogResult result = MessageBox.Show("test", "bla bla", MessageBoxButtons.OKCancel);

if (result == DialogResult.OK)
    MessageBox.Show("Ok was selected");

Constructor could be a problem

I'm not an WPF expert, but there could be another problem: Your code-example shows that you're opening a MessageBox within your constructor. This can be a problem when showing your Form twice or if the code which handles your selection crashed.

Think about moving this to an Event when your Form is loaded:

public MainWindow()
{
    InitializeComponent();
    this.IsEnabled = false;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    MessageBoxResult result = MessageBox.Show("asd", "xcvxcv", MessageBoxButton.OKCancel);

    if (result == MessageBoxResult.OK)
        MessageBox.Show("Ok was selected");

    this.IsEnabled = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM