Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Custom dialog box using Enumeration

0.00/5 (No votes)
9 Jun 2008 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here