Click here to Skip to main content
15,390,470 members
Articles / Desktop Programming / Windows Forms
Posted 9 Jun 2008


9 bookmarked

Creating Custom dialog box using Enumeration

Rate me:
Please Sign up or sign in to vote.
1.31/5 (6 votes)
9 Jun 2008CPOL2 min read
Creating Custom dialog box using Enumeration


The idea is to explain Enumeration with some live example. Have you seen an inbuilt dialog box with “No To All” button. While I was working in one of my project I needed a dialog box having “No”, “No To All”, Yes , Yes To All etc buttons. We can easily create such dialog box using a winform and C# enumeration.


Enums are value types that are created on stack but not on the heap. They require fewer resources from both the processor and the memory on which the application is running. You don'nt have to use new to create an enum type.

public enum Seasons { winter, summer, autumn} 

An Enum is a named constant whose underlying type is any integral type except char. If no underlying type declared, Int32 is used. C# typically provide syntax to declare an enumeration that consists of a set of named constants and their values.

public enum Seasons { Winter = 1, summer = 2, autumn = 3 }

The assignment of numeric values is optional.By default the number begins with zero, but this may be overriden. Dot notation is used to access individual enumerators.

Enumerator allows code to look a lot cleaner and easier to read. Enum help us to get rid of numbers which have a purpose within a module of code, but make the code harder to read.

Let’s discuss some of the disadvantage of using Constants or number directly.

public void ShowSeason(int option)


          case 1 :


          case 2 :



          case 3 : 



For displaying winter we have to call ShowSeason like this


A Few problems with this kind of code:

  1. Not readable : If we see the code, its really difficult to get what 1, 2 and 3 are for?
  2. Not Typesafe : These are not type safe. Instead of passing 1, we can pass number like -999, which is meaningless in our context.

There are many more disadvantages. Lets convert the above code usning Enum and see how it solves the above problem.

Declare Enum :

Public enum Seasons {Winterm, Summer, Autumn}

Use above enum in function "ShowSeason"

public void ShowSeason(Seasons s) // It accepts parameter of type Seasons only.
       {                    // Cannot pass arbitrary number like -999. Hence
    Switch(s)                //type safe.
       case Seasons.winter :
         case Seasons.Summer :
         case Seasons.Autumn :

Call the function like this :


Advantage :

  1. More Readable
  2. Fully type safe.

Let's make a Reusable Dialog Box Using Enumeration

Open a class library and drag a form(MyDialog.cs). Take few buttons on the form and named them as btnYes, btnNo, btnNoToAll etc. Also drag a label (lblMsg) on it to display message. Write following codes in the code behind of MyDialog.cs.

Public partial class MyDialog : Form

    public enum Clicked{Yes,No, NoToAll} // Declaring an enum
    private Clicked ClickedResult = Clicked.No; //Declare a variable of type Clicked

                                                     // and assigned default value.
         Public Clicked prtClickedResult       // Public property for identifying which
     {                      // Which button is clicked.
            get { return ClickedResult ;}
         Public MyDialog(string strMsg)
              lblMsg.Text = strMsg;        //Assigning the message say "Wanna delete"
         Private void btnYes_Clicked(object sender, EventArgs e)     
              ClickedResult = Clicked.Yes;
         this.close();            // Closing the dialog
         private void btnNoToAll_Clicked(object sender,EventArgs e)
             ClickedResult = Clicked.NoToAll;
         // Similarly you can write for any type of buttons.

Most of the things are self explanatory. Here, whenever the user clicks any button, the enumeration value of the corresponding button is assigned to "ClickedResult" which will be latter accessed by our appliaction through the public property "prtClickedResult".

For using this, built the class library and include the dll to your application and write following codes :

string strMsg = "Are you sure you want to delete this file"
       MyDialogs.MyDialog obj = new MyDialogs.MyDialog(strMsg); // Creating object of 
                                               // dialog which is in the dll.
       obj.ShowDialog();          // Show the form as Dialog

       // Now here we have to check which button user has clicked 
       MyDialogs.MyDialog.Clicked objEnum = obj.prtClickedResult; 
                             // getting which button user has clicked using public 
                             // property which is in our dll.
       if(objEnum == MyDialogs.MyDialog.Clicked.Yes)        
            // Write code for Yes condition
       else if (objEnum == MyDialogs.MyDialog.Clicked.NoToAll)
            // Write code for No To All condition

       // Like this write coditions according to the number and type of button you 
      // have in the dll.

Happy Coding !!!!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Suraj Rai
Software Developer (Senior) Sapient Corporation
India India
Holding a MCA degree, trying to be a small part of this big IT world.

Listen to others if they correct me, I am not an expert.

Comments and Discussions

GeneralMy vote of 1 Pin
C.V.Vikram3-Sep-09 0:19
MemberC.V.Vikram3-Sep-09 0:19 
GeneralOne more thing, Pin
ring_010-Jun-08 19:44
Memberring_010-Jun-08 19:44 
You wrote:

public void ShowSeason(Seasons s) // It accepts parameter of type Seasons only.
       {                    // Cannot pass arbitrary number like -999. Hence
    Switch(s)                //type safe.

I can pass arbitary number like 654654,see how I do this
Session sess= (Session)654654;

Man having 1 bit brain with parity error

GeneralAnother one so called article Pin
Selvin10-Jun-08 4:30
MemberSelvin10-Jun-08 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.