So in the past few days I've been trying to achieve two objectives -
- Improve mobile application user interfaces.
- Brand my company (Anglia).
So with this in mind, I realised that the first candidate and the second could be the same thing. Often in mobile applications, we need to display message boxes. Ah ha, I thought. perfect case for a bit of branding.
So instead of doing -
MessageBox.Show("Hello World");
I added my own class to provide a branded message box, like
MessageBoxAnglia.Show("Hello World");
Here's the result -
Of course just like the usual MessageBox we can do all the normal questions and stuff, like.
MessageBoxAnglia.Show("Is This Any Good?","Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
So how's this done. Well quite simply I have just build a custom form, with our branded messagebox.
The code is fairly simple. I'll give you the code behind the form so that you can design your own. Before I do, I think I have achieved both objectives, branding and a slightly improved user interface.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AngliaTemplate
{
public partial class MessageBoxAnglia : Form
{
public static DialogResult Show(string text)
{
return Show(text, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(string text, string caption)
{
return Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultbutton)
{
bool switchedoffwait = false;
if (Cursor.Current == Cursors.WaitCursor)
{
switchedoffwait = true;
Cursor.Current = Cursors.Default;
Application.DoEvents();
}
MessageBoxAnglia mb = new MessageBoxAnglia();
mb.Text = caption;
mb.Message = text;
mb.Buttons = buttons;
mb.FormIcon = icon;
mb.DefaultButton = defaultbutton;
DialogResult dr = mb.ShowDialog();
Application.DoEvents();
mb.Dispose();
if (switchedoffwait)
{
Cursor.Current = Cursors.WaitCursor;
Application.DoEvents();
}
return dr;
}
private string message = "";
private MessageBoxButtons buttons = MessageBoxButtons.OK;
private MessageBoxIcon formicon = MessageBoxIcon.None;
private MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button1;
public string Message
{
set
{
message = value;
}
}
public MessageBoxButtons Buttons
{
set
{
buttons = value;
}
}
public MessageBoxIcon FormIcon
{
set
{
formicon = value;
}
}
public MessageBoxDefaultButton DefaultButton
{
set
{
defaultbutton = value;
}
}
public MessageBoxAnglia()
{
InitializeComponent();
}
private void btok_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void MessageBoxAnglia_Load(object sender, EventArgs e)
{
if (this.Text == "")
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
this.label1.Text = message;
if (message.Length > 100)
{
Font f = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
this.label1.Font = f;
}
this.btyes_from_yes_no_cancel.Visible = false;
this.btno_from_yes_no_cancel.Visible = false;
this.btcancel_from_yes_no_cancel.Visible = false;
switch (buttons)
{
case MessageBoxButtons.OK:
this.mnucancel.Text = "";
break;
case MessageBoxButtons.OKCancel:
case MessageBoxButtons.RetryCancel:
case MessageBoxButtons.YesNo:
if (buttons == MessageBoxButtons.RetryCancel)
{
this.mnuok.Text = "Retry";
}
if (buttons == MessageBoxButtons.YesNo)
{
this.mnuok.Text = "Yes";
this.mnucancel.Text = "No";
}
break;
case MessageBoxButtons.AbortRetryIgnore:
case MessageBoxButtons.YesNoCancel:
this.mainMenu1.MenuItems.Clear();
this.btyes_from_yes_no_cancel.Visible = true;
this.btno_from_yes_no_cancel.Visible = true;
this.btcancel_from_yes_no_cancel.Visible = true;
if (buttons == MessageBoxButtons.AbortRetryIgnore)
{
this.btyes_from_yes_no_cancel.Text = "Abort";
this.btno_from_yes_no_cancel.Text = "Retry";
this.btcancel_from_yes_no_cancel.Text = "Ignore";
}
break;
}
// icon
switch (formicon)
{
case MessageBoxIcon.Asterisk:
this.pbicon.Image = loadimage("Asterisk.png");
break;
case MessageBoxIcon.Exclamation:
this.pbicon.Image = loadimage("Exclamation.png");
break;
case MessageBoxIcon.Hand:
this.pbicon.Image = loadimage("Hand.png");
break;
case MessageBoxIcon.Question:
this.pbicon.Image = loadimage("Question.png");
break;
default:
this.pbicon.Visible = false;
break;
}
// default button
switch (buttons)
{
case MessageBoxButtons.OKCancel:
case MessageBoxButtons.RetryCancel:
case MessageBoxButtons.YesNo:
if (defaultbutton == MessageBoxDefaultButton.Button2)
{
//this.mnucancel.Focus();
}
else
{
//this.mnuok.Focus();
}
break;
case MessageBoxButtons.AbortRetryIgnore:
case MessageBoxButtons.YesNoCancel:
switch (defaultbutton)
{
case MessageBoxDefaultButton.Button1:
this.btyes_from_yes_no_cancel.Focus();
break;
case MessageBoxDefaultButton.Button2:
this.btno_from_yes_no_cancel.Focus();
break;
case MessageBoxDefaultButton.Button3:
this.btcancel_from_yes_no_cancel.Focus();
break;
}
break;
}
}
private Bitmap loadimage(string imageName)
{
return new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().
GetManifestResourceStream("AngliaTemplate.Images." + imageName));
}
private void mnuok_Click(object sender, EventArgs e)
{
DialogResult dr = DialogResult.None;
switch (buttons)
{
case MessageBoxButtons.RetryCancel:
dr = DialogResult.Retry;
break;
case MessageBoxButtons.YesNo:
dr = DialogResult.Yes;
break;
default:
dr = DialogResult.OK;
break;
}
this.DialogResult = dr;
}
private void mnucancel_Click(object sender, EventArgs e)
{
DialogResult dr = DialogResult.None;
switch (buttons)
{
case MessageBoxButtons.YesNo:
dr = DialogResult.No;
break;
case MessageBoxButtons.OK:
return;
default:
dr = DialogResult.Cancel;
break;
}
this.DialogResult = dr;
}
private void btyes_from_yes_no_cancel_Click(object sender, EventArgs e)
{
DialogResult dr = DialogResult.Yes;
if (buttons == MessageBoxButtons.AbortRetryIgnore)
{
dr = DialogResult.Abort;
}
this.DialogResult = dr;
}
private void btno_from_yes_no_cancel_Click(object sender, EventArgs e)
{
DialogResult dr = DialogResult.No;
if (buttons == MessageBoxButtons.AbortRetryIgnore)
{
dr = DialogResult.Retry;
}
this.DialogResult = dr;
}
private void btcancel_from_yes_no_cancel_Click(object sender, EventArgs e)
{
DialogResult dr = DialogResult.Cancel;
if (buttons == MessageBoxButtons.AbortRetryIgnore)
{
dr = DialogResult.Ignore;
}
this.DialogResult = dr;
}
private void MessageBoxAnglia_Click(object sender, EventArgs e)
{
if (buttons == MessageBoxButtons.OK)
{
this.DialogResult = DialogResult.OK;
}
}
}
}