C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We use a switch selection statement on the DialogResult enum local variable.
Text: 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.
C# program that uses switch on DialogResult
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;
}
}
}
}
}
Also: I would like to know how OK and Cancel could be clicked at the same time.
Enum values:
DialogResult.None
DialogResult.OK
DialogResult.Cancel
DialogResult.Abort
DialogResult.Retry
DialogResult.Ignore
DialogResult.Yes
DialogResult.No