Search code examples
c#winformsmessagebox

Message Box Icon -> Variable?


I'm working on making a MessageBox Method, that I can call quickly thoughout my program, without having to create multiple MessageBox codes, but what I've run into, is when running it everything has the same icon which currently is Error.

Is there any way to Dynamically change the Icon within the method when called?

This is what I have so far and works perfectly.

    private void MB(string Text, String Title)
    {
        MessageBox.Show(Text, Title,
                System.Windows.Forms.MessageBoxButtons.OKCancel,
                System.Windows.Forms.MessageBoxIcon.Error);
    }

What I'm hoping to do is to create:

        private void MB(string Text, String Title, Variable ICON)
    {
        MessageBox.Show(Text, Title,
                System.Windows.Forms.MessageBoxButtons.OKCancel,
                System.Windows.Forms.MessageBoxIcon.ICON);
    }

And be able to call it with:

MB("String 1", "String 2", Error);
MB("String 1", "String 2", Question);

Not sure if this is possible?

Thanks for the help :D


Solution

  • The icon is an enum, so you can do it like:

    private void MB(string Text, String Title, MessageBoxIcon ICON)
    {
        MessageBox.Show(Text, Title,
                MessageBoxButtons.OKCancel,
                ICON);
    }
    

    and you can use it like:

    MB("String 1", "String 2", MessageBoxIcon.Error);