Introduction
I have tried to create a DLL that generates a message box from the added reference. Hope it's worthy.
Background
In the regular message box provided by Microsoft, I wasn't able to get details of the exception and if I provide any, a giant messagebox would appear which doesn't look good and suitable. For that purpose, I created a DLL which draws to message boxes:
- A general message box like the one Microsoft already provides but the only difference it has is the text box which appears if you provide a description in the show function of the message box, otherwise it's hidden.
- Exception Box where you only have to pass the exception object and rest it will do.
I am eagerly waiting for any suggestions or improvements for this job. Anyone having some, 'More Than Welcome'.
Using the Code
I used panel to contain the controls because whatever theme or appearances will be applied on panel will be applicable on all the controls within it. Thus appearances and theme will be the same for each of the controls.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text
namespace nameMSGBOX
{
#region Enums
public enum MSGICON
{
Error,
Information,
Warning,
Question,
}
public enum MSGBUTTON
{
None,
OK,
YesNo,
YesNoCancel,
RetryCancle,
AbortRetryIgnore
}
public enum MSGRESPONSE
{
None,
Yes,
No,
OK,
Abort,
Retry,
Ignore,
Cancel
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Reflection;
namespace nameMSGBOX
{
public class MSGBOX
{
#region initialize controls
public Form frm = new Form();
internal Button btnDetails = new Button();
Button btnOK = new Button();
Button btnYes = new Button();
Button btnNo = new Button();
Button btnCancel = new Button();
Button btnAbort = new Button();
Button btnRetry = new Button();
Button btnIgnore = new Button();
TextBox txtDescription = new TextBox();
PictureBox icnPicture = new PictureBox();
Panel formpanel = new Panel();
Label lblmessage = new Label();
static MSGRESPONSE msgresponse = new MSGRESPONSE();
#endregion initialize controls
#region DrawingFunction
internal void DrawBox()
{
frm.Controls.Add(formpanel);
formpanel.Dock = DockStyle.Fill;
icnPicture.Height = 36;
icnPicture.Width = 40;
icnPicture.Location = new Point(10, 11);
formpanel.Controls.Add(icnPicture);
txtDescription.Multiline = true;
txtDescription.Height = 183;
txtDescription.Width = 464;
txtDescription.Location = new Point(6, 143);
txtDescription.BorderStyle = BorderStyle.Fixed3D;
txtDescription.ScrollBars = ScrollBars.Both;
txtDescription.ReadOnly = true;
formpanel.Controls.Add(txtDescription);
btnDetails.Height = 24;
btnDetails.Width = 80;
btnDetails.Location = new Point(6, 114);
btnDetails.Tag = "exp";
btnDetails.Text = "Show Details";
formpanel.Controls.Add(btnDetails);
this.btnDetails.Click += new EventHandler(this.btnDetails_click);
lblmessage.Location = new Point(64, 22);
lblmessage.AutoSize = true;
formpanel.Controls.Add(lblmessage);
frm.Height = 360;
frm.Width = 483;
frm.StartPosition = FormStartPosition.CenterScreen;
frm.FormBorderStyle = FormBorderStyle.FixedSingle;
frm.MaximizeBox = false;
frm.MinimizeBox = false;
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.BackColor = System.Drawing.SystemColors.ButtonFace;
frm.Icon = ((System.Drawing.Icon)(nameMSGBOX.Properties.Resources.P));
if (btnDetails.Tag.ToString() == "exp")
{
frm.Height = frm.Height - txtDescription.Height - 6;
btnDetails.Tag = "col";
btnDetails.Text = "Show Details";
}
}
private void AddButton(MSGBUTTON MSGBTN)
{
switch (MSGBTN)
{
case MSGBUTTON.OK:
{
btnOK.Width = 80;
btnOK.Height = 24;
btnOK.Location = new Point(391, 114);
btnOK.Text = "OK";
formpanel.Controls.Add(btnOK);
btnOK.Click += new EventHandler(Return_Response);
}
break;
case MSGBUTTON.YesNo:
{
btnNo.Width = 80;
btnNo.Height = 24;
btnNo.Location = new Point(391, 114);
btnNo.Text = "No";
formpanel.Controls.Add(btnNo);
btnNo.Click += new EventHandler(Return_Response);
btnYes.Width = 80;
btnYes.Height = 24;
btnYes.Location = new Point(btnNo.Location.X - btnNo.Width - 2, 114);
btnYes.Text = "Yes";
formpanel.Controls.Add(btnYes);
btnYes.Click += new EventHandler(Return_Response);
}
break;
case MSGBUTTON.YesNoCancel:
{
btnCancel.Width = 80;
btnCancel.Height = 24;
btnCancel.Location = new Point(391, 114);
btnCancel.Text = "Cancel";
formpanel.Controls.Add(btnCancel);
btnCancel.Click += new EventHandler(Return_Response);
btnNo.Width = 80;
btnNo.Height = 24;
btnNo.Location = new Point(btnCancel.Location.X - btnCancel.Width - 2, 114);
btnNo.Text = "No";
formpanel.Controls.Add(btnNo);
btnNo.Click += new EventHandler(Return_Response);
btnYes.Width = 80;
btnYes.Height = 24;
btnYes.Location = new Point(btnNo.Location.X - btnNo.Width - 2, 114);
btnYes.Text = "Yes";
formpanel.Controls.Add(btnYes);
btnYes.Click += new EventHandler(Return_Response);
}
break;
case MSGBUTTON.RetryCancle:
{
btnCancel.Width = 80;
btnCancel.Height = 24;
btnCancel.Location = new Point(391, 114);
btnCancel.Text = "Cancel";
formpanel.Controls.Add(btnCancel);
btnCancel.Click += new EventHandler(Return_Response);
btnRetry.Width = 80;
btnRetry.Height = 24;
btnRetry.Location = new Point(btnCancel.Location.X - btnCancel.Width - 2, 114);
btnRetry.Text = "Retry";
formpanel.Controls.Add(btnRetry);
btnRetry.Click += new EventHandler(Return_Response);
}
break;
case MSGBUTTON.AbortRetryIgnore:
{
btnIgnore.Width = 80;
btnIgnore.Height = 24;
btnIgnore.Location = new Point(391, 114);
btnIgnore.Text = "Ignore";
formpanel.Controls.Add(btnIgnore);
btnIgnore.Click += new EventHandler(Return_Response);
btnRetry.Width = 80;
btnRetry.Height = 24;
btnRetry.Location = new Point(btnIgnore.Location.X - btnIgnore.Width - 2, 114);
btnRetry.Text = "Retry";
formpanel.Controls.Add(btnRetry);
btnRetry.Click += new EventHandler(Return_Response);
btnAbort.Width = 80;
btnAbort.Height = 24;
btnAbort.Location = new Point(btnRetry.Location.X - btnRetry.Width - 2, 114);
btnAbort.Text = "Abort";
formpanel.Controls.Add(btnAbort);
btnAbort.Click += new EventHandler(Return_Response);
}
break;
case MSGBUTTON.None:
{
btnOK.Width = 80;
btnOK.Height = 24;
btnOK.Location = new Point(391, 114);
btnOK.Text = "OK";
formpanel.Controls.Add(btnOK);
btnOK.Click += new EventHandler(Return_Response);
}
break;
}
}
internal void AddIconImage(MSGICON MSGICON)
{
switch (MSGICON)
{
case MSGICON.Error:
icnPicture.Image = SystemIcons.Error.ToBitmap(); break;
case MSGICON.Information:
icnPicture.Image = SystemIcons.Information.ToBitmap();
break;
case MSGICON.Question:
icnPicture.Image = SystemIcons.Question.ToBitmap();
break;
case MSGICON.Warning:
icnPicture.Image = SystemIcons.Warning.ToBitmap();
break;
default:
icnPicture.Image = SystemIcons.Information.ToBitmap();
break;
}
}
#endregion DrawingFunction
#region EVents
private void btnDetails_click(object sender, EventArgs e)
{
if (btnDetails.Tag.ToString() == "col")
{
frm.Height = frm.Height + txtDescription.Height + 6;
btnDetails.Tag = "exp";
btnDetails.Text = "Hide Details";
txtDescription.WordWrap = true;
}
else if (btnDetails.Tag.ToString() == "exp")
{
frm.Height = frm.Height - txtDescription.Height - 6;
btnDetails.Tag = "col";
btnDetails.Text = "Show Details";
}
}
private void frm_FormClosing(object sender, EventArgs e)
{
if (msgresponse == MSGRESPONSE.None)
{
msgresponse = MSGRESPONSE.Cancel;
}
}
private void Return_Response(object sender, EventArgs e)
{
Button btn = (Button)sender;
string buttonText = btn.Text;
if (buttonText == "Yes")
{
msgresponse = MSGRESPONSE.Yes;
}
else if (buttonText == "No")
{
msgresponse = MSGRESPONSE.No;
}
else if (buttonText == "Cancel")
{
msgresponse = MSGRESPONSE.Cancel;
}
else if (buttonText == "OK")
{
msgresponse = MSGRESPONSE.OK;
}
else if (buttonText == "Abort")
{
msgresponse = MSGRESPONSE.Abort;
}
else if (buttonText == "Retry")
{
msgresponse = MSGRESPONSE.Retry;
}
else if (buttonText == "Ignore")
{
msgresponse = MSGRESPONSE.Ignore;
}
else
{
msgresponse = MSGRESPONSE.Cancel;
}
frm.Dispose();
}
#endregion Events
#region Overloaded Show message to display message box.
public static MSGRESPONSE Show(string messageText)
{
MSGBOX frmMessage = new MSGBOX();
frmMessage.SetMessageText(messageText, "", null);
frmMessage.AddIconImage(MSGICON.Information);
frmMessage.AddButton(MSGBUTTON.OK);
frmMessage.DrawBox();
frmMessage.frm.ShowDialog();
return msgresponse;
}
public static MSGRESPONSE Show(string messageText, string messageTitle, string description)
{
MSGBOX frmMessage = new MSGBOX();
frmMessage.SetMessageText(messageText, messageTitle, description);
frmMessage.AddIconImage(MSGICON.Information);
frmMessage.AddButton(MSGBUTTON.OK);
frmMessage.DrawBox();
frmMessage.frm.ShowDialog();
return msgresponse;
}
public static MSGRESPONSE Show(string messageText,
string messageTitle, string description, MSGICON IcOn, MSGBUTTON btn)
{
MSGBOX frmMessage = new MSGBOX();
frmMessage.SetMessageText(messageText, messageTitle, description);
frmMessage.AddIconImage(IcOn);
frmMessage.AddButton(btn);
frmMessage.DrawBox();
frmMessage.frm.ShowDialog();
return msgresponse;
}
public static MSGRESPONSE ShowException(Exception ex)
{
MSGBOX frmMessage = new MSGBOX();
frmMessage.SetMessageText(ex.Message, ex.Message, ex.StackTrace);
frmMessage.AddIconImage(MSGICON.Error);
frmMessage.AddButton(MSGBUTTON.OK);
frmMessage.DrawBox();
frmMessage.frm.ShowDialog();
return msgresponse;
}
#endregion Overloaded Show message to display message box.
#region Private Methodes to set messagebox Display
private void SetMessageText(string messageText, string Title, string Description)
{
this.lblmessage.Text = messageText;
if (!string.IsNullOrEmpty(Description))
{
this.txtDescription.Text = Description;
}
else
{
btnDetails.Visible = false;
}
if (!string.IsNullOrEmpty(Title))
{
frm.Text = Title;
}
else
{
frm.Text = "Your Message Box From DLL";
}
}
#endregion Private Methodes to set messagebox Display
}
}
Points of Interest
- Drawing
messagebox
from class library
History
- 10th October, 2014: First version