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
{
Form answForm;
List<Button> buttons = new List<Button>();
int buttonClicked = 0;
public int show(List<string> choices, Icon icon, Point position)
{ 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.