Home > Software design >  Message Box Icon -> Variable?
Message Box Icon -> Variable?

Time:11-09

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

CodePudding user response:

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

CodePudding user response:

Perhaps create a language extension, in this case there are several to choose from.

In this case the following code is in a form, the source code is in a class project WindowFormsHelpersLibrary so to use them add a static using statement.

Note several also set the default button to No which I've found to be useful for those users who don't pay attention to say asking to delete a record for instance.

Form code

using static WindowFormsHelpersLibrary.LanguageExtensions.Dialogs;

using MessageBox = System.Windows.Forms.MessageBox;

if (MessageBox("Whatever to say", "Demo", MessageBoxIcon.Asterisk))
{
    MessageBox.Show("Ok");
}
else
{
    MessageBox.Show("Cancelled");
}

if (MessageBox("Whatever to say"))
{
    MessageBox.Show("Ok");
}
else
{
    MessageBox.Show("Cancelled");
}

if (Question("Continue with processing"))
{
    MessageBox.Show("Ok");
}
else
{
    MessageBox.Show("Cancelled");
}

InformationDialog("Operation completed","Bulk upload", MessageBoxIcon.Exclamation);

Source in class project

using System.Diagnostics;
using System.Windows.Forms;
using static System.Windows.Forms.MessageBox;

namespace WindowFormsHelpersLibrary.LanguageExtensions
{
    public static class Dialogs
    {
        [DebuggerStepThrough]
        public static bool Question(string text) =>
            (Show(text, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
                MessageBoxDefaultButton.Button2) == DialogResult.Yes);
        
        [DebuggerStepThrough]
        public static bool Question(string text, MessageBoxIcon icon) =>
            (Show(text, Application.ProductName, MessageBoxButtons.YesNo, icon, 
                MessageBoxDefaultButton.Button2) == DialogResult.Yes);


        [DebuggerStepThrough]
        public static void InformationDialog(string message, string title) =>
            Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);

        [DebuggerStepThrough]
        public static void InformationDialog(string message, string title, MessageBoxIcon icon) =>
            Show(message, title, MessageBoxButtons.OK, icon);


        [DebuggerStepThrough]
        public static void ErrorDialog(string message) =>
            Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);


        [DebuggerStepThrough]
        public static void ErrorDialog(string message, string title) =>
            Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);


        [DebuggerStepThrough]
        public static bool MessageBox(string text, MessageBoxIcon icon = MessageBoxIcon.Error) =>
            Show(text, Application.ProductName, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;

        [DebuggerStepThrough]
        public static bool MessageBox(string text,string title, MessageBoxIcon icon = MessageBoxIcon.Error) => 
            Show(text, title, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;
    }
}

Or a simpler version

public static class Dialogs
{
    [DebuggerStepThrough]
    public static bool MsgBox(string text, MessageBoxIcon icon = MessageBoxIcon.Error) =>
        MessageBox.Show(text, Application.ProductName, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;

    [DebuggerStepThrough]
    public static bool MsgBox(string text, string title, MessageBoxIcon icon = MessageBoxIcon.Error) =>
        MessageBox.Show(text, title, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;
}

Usage

if (MsgBox("Continue?"))
{
    MessageBox.Show("Yes");
}
else
{
    MessageBox.Show("No");
}
if (MsgBox("Continue?", MessageBoxIcon.Question))
{
    MessageBox.Show("Yes");
}
else
{
    MessageBox.Show("No");
}
  • Related