TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DialogResult: Windows Forms

This C# program uses the DialogResult enum in Windows Forms.

DialogResult 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. It is a value. It can be switched upon and tested in an if-statement.

Example. This is a Windows Forms program that uses a Form1_Load event handler. We show a MessageBox with the OK and Cancel buttons (seen in the screenshot). We use the MessageBox.Show method for this.

MessageBox.Show

Afterwards, we use a switch selection statement on the DialogResult enum local variable. We change the Text property based on what button was clicked in the MessageBox. If the Enter key was pressed, the OK button is used.

Note: The output of this program depends on the button pressed by the user. Try it a few times.

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;
		    }
	    }
	}
    }
}

Values. The DialogResult enum contains many different values. Because this enum is not decorated with the [Flags] attribute, you cannot combine multiple enum values. Here are the possible values for DialogResult.

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

Summary. We examined the DialogResult enumerated constant in the Windows Forms graphical user interface toolkit. With this enum we receive an encoded form of the action taken by the user, which can then influence flow control.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf