开发者

C# DialogBox and DialogResult

开发者 https://www.devze.com 2023-01-05 11:19 出处:网络
I want to get the button of the DialogBox that the user clicked 开发者_开发百科... yet when I use the DialogResult I get this error

I want to get the button of the DialogBox that the user clicked 开发者_开发百科... yet when I use the DialogResult I get this error

'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'

How can I use the DialogResult??

Ok, I`ve managed to solve it.

MessageBoxResult Result = MessageBox.Show("Message Body", @"Caption/Title", MessageBoxButton.YesNo);
        switch (Result)
        {
            case MessageBoxResult.Yes:
                MessageBox.Show("Yes Pressed!!");
                break;
            case MessageBoxResult.No:
                MessageBox.Show("No Pressed!!");
                break;
        }


Update: Just realised you're using WPF, not WinForms. Here's a correct implementation of DialogResult within WPF:

MyDialog dialog = new MyDialog();
bool? dialogResult = dialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
   // User clicked OK
}
else
{
   // User clicked Cancel"
}

There's a good tutorial on it here.

Just sounds like you're using the Form's DialogResult property incorrectly. You should be doing something like the following:

DialogResult result = Form.DialogResult;
if (result == DialogResult.Yes)
{
   // Do something
}

You can find the full DialogResult enumeration breakdown here.


Do you need a DialogBox? Or would a MessageBox work for your purposes?

 DialogResult dlg = MessageBox.Show("Question User?",
                   "MessageBox Title",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question);
            if (dlg == DialogResult.No)
            {
                //user changed mind. return
                return;
            }

etc.


DialogResult is Enum - you can directly compare with the DialogResult Property of your form.


If you are using WPF or Silverlight then DialogResult is a bool? and you can use ?? to supply a value if the result is null.

if (myWindow.DialogResult ?? false)
    Debug.WriteLine("You clicked OK");
else
    Debug.WriteLine("You clicked Cancel");


You're using WPF's DialogResult property, which is a Nullable<bool>, not an enumeration.

You need to check the result like so:

bool? dialogResult = dialogBox.ShowDialog();

if (dialogResult.HasValue) // Should always have a value, at this point, since the dialogBox.ShowDialog() returned at this point.  Will be false until the dialog is closed, however
{
    if (dialogResult.Value)
    {
        // User "accepted" the dialog, hitting yes, OK, etc...
    }
    else
    {
        // User hit "cancel" button
    }
}


C# DialogBox and DialogResult

    {

        DialogResult a1 = MessageBox.Show("Test", "Title", MessageBoxButtons.YesNo);
        if (a1 == DialogResult.Yes)
            MessageBox.Show("Yes");
        else if (a1 == DialogResult.No)
            MessageBox.Show("No");

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消