DialogResult. This is returned by dialogs after dismissal. It indicates which button was clicked on the dialog by the user. It is used with the MessageBox.Show method.
Type info. DialogResult is a value. It can be switched upon and tested in an if-statement. Usually, we test its value after calling a method that returns it.
Example program. Here show a MessageBox with the OK and Cancel buttons (seen in the screenshot). We use the MessageBox.Show method for this.
Then We use a switch selection statement on the DialogResult enum local variable.
Detail We change the Text property based on what button was clicked in the MessageBox.
And If the Enter key was pressed, the OK button is used. The output of this program depends on the button pressed by the user.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("How are you?", "Hi",
MessageBoxButtons.OKCancel);
switch (result)
{
case DialogResult.OK:
{
this.Text = "[OK]";
break;
}
case DialogResult.Cancel:
{
this.Text = "[Cancel]";
break;
}
}
}
}
}
Values. The DialogResult enum contains many different values. Because this enum is not decorated with the [Flags] attribute, you cannot combine multiple enum values.
Summary. We examined the DialogResult enum. With this enum we receive an encoded form of the action taken by the user, which can then influence flow control.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 25, 2022 (edit).