Click here to Skip to main content
11,437,816 members (38,545 online)
Click here to Skip to main content

Creating Custom dialog box using Enumeration

, 9 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Creating Custom dialog box using Enumeration

Introduction

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.

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)
      {

        switch(option)
        {

          case 1 :

           MessageBox.Show("Winter");

           break;
          case 2 :

           MessageBox.Show("summer");

           break;

          case 3 : 

           MessageBox.Show("Autumn");
           break;

        }
     }

For displaying winter we have to call ShowSeason like this

ShowSeason(1);

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 :
            MessageBox.Show("Winter");
            break;
         case Seasons.Summer :
            MessageBox.Show("Summer"); 
            break;
         case Seasons.Autumn :
            MessageBox.Show("Autumn");
            break;
 }          
}

Call the function like this :

  ShowFunction(Seasons.Winter);

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)
         {
              InitializeComponent();
              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;
        this.Close();
         }
         // 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 !!!!

License

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

Share

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. Started my career in the year of 2005.

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 1:19
memberC.V.Vikram3-Sep-09 1:19 
GeneralOne more thing, Pin
ring_010-Jun-08 20:44
memberring_010-Jun-08 20:44 
GeneralAnother one so called article Pin
Selvin10-Jun-08 5:30
memberSelvin10-Jun-08 5:30 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150506.1 | Last Updated 10 Jun 2008
Article Copyright 2008 by Suraj Rai
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid