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

AnswerBox

0.00/5 (No votes)
4 Jan 2013 2  
AnswerBox is just another replacement for the inflexible MessageBox.

Introduction

Besides MessageBox inflexibility respectively its Icons are messed up since years, no one thinks of fixing it, furthermore it is extremely outdated. It just takes a few lines of code to have it done with great flexibility. By the way, it is used in a further project, the OpenFileFolder dialog, but more to that in another session.

Wrongful Icon assignments in SystemIcons

Icon:                             Shows Icon:
SystemIcons.Application           Application
SystemIcons.Asterics              Info
SystemIcons.Error                 Error
SystemIcons.Exclamation           Warning
SystemIcons.Hand                  Error
SystemIcons.Information           Information
SystemIcons.Question              Question
SystemIcons.Shield                Shield
SystemIcons.Warning               Warning
SystemIcons.Winlogo               Application Icon

The task

It can not be foreseen at what stage of development what message shall be displayed and what answer shall be chosen, so a simple AnswerBox with messages assigned at design time comes in handy. A string list is ideal to support such a flexible approach.

The calling convention

AnswerBox box = new AnswerBox();
readonly List<string> answerButtons = new List<string> { "Message", "Title", 
         "Continue", "Yes to all", " No to all", "Cancel" };

int z = box.show(answerButtons, SystemIcons.Exclamation, new Point(15, 15));

or

string Text = answerButtons[box.show(answerButtons, SystemIcons.Exclamation, new Point(10, 10))];

The Code

The code for AnswerBox is so small, it can be copied into the source code of if desired and saved into a class.

public class AnswerBox
{
    // a simple display of choices stored as strings in choices
    // choices[0] = message, choices[1] = window title, choices[2...] = button texts
    Form answForm;
    List<Button> buttons = new List<Button>();
    int buttonClicked = 0;

    public int show(List<string> choices, Icon icon, Point position)
    { // show the form modal, it will return the string of the button clicked
        // no empty boxes pls
        if (choices.Count() <= 2)
            return buttonClicked;

        if (answForm == null)
            answForm = new Form();
        answForm.MaximizeBox = false;
        answForm.MinimizeBox = false;

        Label msg = new Label();
        Button bu;
        PictureBox iconButton = new PictureBox();
        iconButton.Width = 32;
        iconButton.Height = 32;

        Bitmap bm = new Bitmap(32, 32);
        Graphics gr = Graphics.FromImage(bm);
        gr.DrawIcon(icon, new Rectangle(0, 0, 32, 32));
        iconButton.Image = bm;
        gr.Dispose();

        iconButton.Top = 10;
        buttons.Clear();
        answForm.Icon = icon;
        msg.AutoSize = true;
        msg.Text = choices[0];
        answForm.Height = 150;
        answForm.Width = 400;
        answForm.Text = choices[1];
        msg.Top = 20; msg.Left = 30;
        answForm.Controls.Add(msg);
        answForm.Left = position.X; answForm.Top = position.Y;

        for (int i = 2; i < choices.Count(); i++)
        {
            bu = new Button();
            bu.Text = choices[i];
            bu.Height = 40;

            bu.Top = answForm.Height - 90;
            bu.Tag = i;
            buttons.Add(bu);
        }

        for (int i = 0; i < buttons.Count(); i++)
        {
            if (i < 1)
                buttons[i].Left = 50;
            else
                buttons[i].Left = buttons[i - 1].Left + buttons[i].Width + 10;
        }

        answForm.Width = buttons[buttons.Count() - 1].Left +
            buttons[buttons.Count() - 1].Width + 30;
        if (msg.Width + 50 > answForm.Width)
        {
            answForm.Width = msg.Width + 50;
            for (int i = buttons.Count() - 1; i >= 0; i--)
                if (i == buttons.Count() - 1)
                    buttons[i].Left = answForm.Width - buttons[i].Width - 30;
                else
                    buttons[i].Left = buttons[i + 1].Left - buttons[i].Width - 10;
        }

        for (int i = 0; i < buttons.Count(); i++)
        {
            buttons[i].Click += new EventHandler(AnswerBox_Click);
            answForm.Controls.Add(buttons[i]);
        }

        iconButton.Left = answForm.Width - 62;
        answForm.Controls.Add(iconButton);
        answForm.ShowDialog();
        answForm.Controls.Clear();
        buttons.Clear();
        bm.Dispose();
        return buttonClicked;
    }

    void AnswerBox_Click(object sender, EventArgs e)
    {
        Button bu = (Button)sender;
        buttonClicked = (int)bu.Tag;
        answForm.Close();
    }
}

Extensions

Of course it can be extended, it’s up to you! The parameter positions have to be specified as screen coordinates. The form ControlBox, MaximizeBox, and MinimizeBox can additionally be disabled by the coder. Presetting an answer button through an additional parameter is also not a bad idea.

Warranty

There is absolute no warranty whatsoever implied or assumed. Use it at your own risk. It does a marvelous job for the author. Copyrights and Trademarks shall belong to their respective owners. I am not going to fight over that!

Motto: It’s not the developer’s duty to pay up for the engineer’s ignorance.

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