Click here to Skip to main content
13,355,018 members (58,157 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 4 Jan 2013


, 4 Jan 2013
Rate this:
Please Sign up or sign in to vote.
AnswerBox is just another replacement for the inflexible MessageBox.


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 =, SystemIcons.Exclamation, new Point(15, 15));


string Text = 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;

        iconButton.Top = 10;
        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.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;

        for (int i = 0; i < buttons.Count(); i++)
            if (i < 1)
                buttons[i].Left = 50;
                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;
                    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);

        iconButton.Left = answForm.Width - 62;
        return buttonClicked;

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


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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Philippines Philippines
Grew up in a metal processing company and did industrial HW/SW development since the birth of Intel’s 8080. Lectured IT since 1986 at several levels. Hobbies, sidesteps: Woodworking and deep sea diving. Background: ASM, C, C++. Platforms: Win, Novel, CP/M, MP/M, DOS, (Linux).

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

You may also be interested in...

Comments and Discussions

GeneralComplete Project Download incl update / bugfix Pin
cyprussun12-Mar-13 20:11
membercyprussun12-Mar-13 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 4 Jan 2013
Article Copyright 2013 by cyprussun
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid