MMD MNC
MMD MNC

Reputation: 159

Use enum to build a custom Messagebox like Messagebox in c#

Use enum to build a custom Messagebox like Messagebox in c#

I am using the following code :

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void Sow()
    {
        CuMessageBox.SowMessage("text", "caption", CuMessageBox.MsgBoxButtons.Yes);

        MessageBox.Show("text", "caption", MessageBoxButtons.OK);
    }
}


public class CuMessageBox
{
    public enum MsgBoxButtons { Yes, No, Ok, Cancel }
    public static void SowMessage(string text, string caption, MsgBoxButtons msg)
    {
        //Todo
    }
}

In this line :

   CuMessageBox.SowMessage("text", "caption", CuMessageBox.MsgBoxButtons.Yes); // Custom MessageBox

I do not want to display the name of the class.How to not show the class name. Message box in C # Will not display the name of the class

I want to use this code to display :

CuMessageBox.SowMessage("text", "caption", MsgBoxButtons.Yes); // Custom MessageBox

error : The name 'MsgBoxButtons' does not exist in the current context

How do I fix this error. With the condition I do not want to write the class name

Upvotes: 0

Views: 1222

Answers (2)

Hans Passant
Hans Passant

Reputation: 941645

You'll have to move the enum out of the class so it is not a nested type.

public enum MsgBoxButtons { Yes, No, Ok, Cancel }
public class CuMessageBox
{
   // etc..
}

Careful with what you are doing, a message box with a single button that says "Yes" or "No" is rather odd. If you want to combine buttons then you'll have to at least use the [Flags] attribute so you can specify more than one button in the Show call:

[Flags]
public enum MsgBoxButtons { 
   Yes = 1,
   No = 2,
   Ok = 4,
   Cancel = 8
}

Or just use the existing MessageBoxButton enum type to avoid all this.

Upvotes: 2

Victor
Victor

Reputation: 666

Can you try to add toString() to the end of your enum. For example MsgBoxButtons.Yes.ToString().

Upvotes: 0

Related Questions