/MessageBoxExDemo/MessageBoxExLib/MessageBoxEx.cs

http://github.com/sijinjoseph/MessageBoxEx · C# · 294 lines · 186 code · 39 blank · 69 comment · 24 complexity · c5beee5ac9f26f1876b115b08c2b598d MD5 · raw file

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace Utils.MessageBoxExLib
  5. {
  6. /// <summary>
  7. /// An extended MessageBox with lot of customizing capabilities.
  8. /// </summary>
  9. public class MessageBoxEx
  10. {
  11. #region Fields
  12. private MessageBoxExForm _msgBox = new MessageBoxExForm();
  13. private bool _useSavedResponse = true;
  14. private string _name = null;
  15. #endregion
  16. #region Properties
  17. internal string Name
  18. {
  19. get{ return _name; }
  20. set{ _name = value; }
  21. }
  22. /// <summary>
  23. /// Sets the caption of the message box
  24. /// </summary>
  25. public string Caption
  26. {
  27. set{_msgBox.Caption = value;}
  28. }
  29. /// <summary>
  30. /// Sets the text of the message box
  31. /// </summary>
  32. public string Text
  33. {
  34. set{_msgBox.Message = value;}
  35. }
  36. /// <summary>
  37. /// Sets the icon to show in the message box
  38. /// </summary>
  39. public Icon CustomIcon
  40. {
  41. set{_msgBox.CustomIcon = value;}
  42. }
  43. /// <summary>
  44. /// Sets the icon to show in the message box
  45. /// </summary>
  46. public MessageBoxExIcon Icon
  47. {
  48. set{ _msgBox.StandardIcon = (MessageBoxIcon)Enum.Parse(typeof(MessageBoxIcon), value.ToString());}
  49. }
  50. /// <summary>
  51. /// Sets the font for the text of the message box
  52. /// </summary>
  53. public Font Font
  54. {
  55. set{_msgBox.Font = value;}
  56. }
  57. /// <summary>
  58. /// Sets or Gets the ability of the user to save his/her response
  59. /// </summary>
  60. public bool AllowSaveResponse
  61. {
  62. get{ return _msgBox.AllowSaveResponse; }
  63. set{ _msgBox.AllowSaveResponse = value; }
  64. }
  65. /// <summary>
  66. /// Sets the text to show to the user when saving his/her response
  67. /// </summary>
  68. public string SaveResponseText
  69. {
  70. set{_msgBox.SaveResponseText = value; }
  71. }
  72. /// <summary>
  73. /// Sets or Gets wether the saved response if available should be used
  74. /// </summary>
  75. public bool UseSavedResponse
  76. {
  77. get{ return _useSavedResponse; }
  78. set{ _useSavedResponse = value; }
  79. }
  80. /// <summary>
  81. /// Sets or Gets wether an alert sound is played while showing the message box.
  82. /// The sound played depends on the the Icon selected for the message box
  83. /// </summary>
  84. public bool PlayAlsertSound
  85. {
  86. get{ return _msgBox.PlayAlertSound; }
  87. set{ _msgBox.PlayAlertSound = value; }
  88. }
  89. /// <summary>
  90. /// Sets or Gets the time in milliseconds for which the message box is displayed.
  91. /// </summary>
  92. public int Timeout
  93. {
  94. get{ return _msgBox.Timeout; }
  95. set{ _msgBox.Timeout = value; }
  96. }
  97. /// <summary>
  98. /// Controls the result that will be returned when the message box times out.
  99. /// </summary>
  100. public TimeoutResult TimeoutResult
  101. {
  102. get{ return _msgBox.TimeoutResult; }
  103. set{ _msgBox.TimeoutResult = value; }
  104. }
  105. #endregion
  106. #region Methods
  107. /// <summary>
  108. /// Shows the message box
  109. /// </summary>
  110. /// <returns></returns>
  111. public string Show()
  112. {
  113. return Show(null);
  114. }
  115. /// <summary>
  116. /// Shows the messsage box with the specified owner
  117. /// </summary>
  118. /// <param name="owner"></param>
  119. /// <returns></returns>
  120. public string Show(IWin32Window owner)
  121. {
  122. if(_useSavedResponse && this.Name != null)
  123. {
  124. string savedResponse = MessageBoxExManager.GetSavedResponse(this);
  125. if( savedResponse != null)
  126. return savedResponse;
  127. }
  128. if(owner == null)
  129. {
  130. _msgBox.ShowDialog();
  131. }
  132. else
  133. {
  134. _msgBox.ShowDialog(owner);
  135. }
  136. if(this.Name != null)
  137. {
  138. if(_msgBox.AllowSaveResponse && _msgBox.SaveResponse)
  139. MessageBoxExManager.SetSavedResponse(this, _msgBox.Result);
  140. else
  141. MessageBoxExManager.ResetSavedResponse(this.Name);
  142. }
  143. else
  144. {
  145. Dispose();
  146. }
  147. return _msgBox.Result;
  148. }
  149. /// <summary>
  150. /// Add a custom button to the message box
  151. /// </summary>
  152. /// <param name="button">The button to add</param>
  153. public void AddButton(MessageBoxExButton button)
  154. {
  155. if(button == null)
  156. throw new ArgumentNullException("button","A null button cannot be added");
  157. _msgBox.Buttons.Add(button);
  158. if(button.IsCancelButton)
  159. {
  160. _msgBox.CustomCancelButton = button;
  161. }
  162. }
  163. /// <summary>
  164. /// Add a custom button to the message box
  165. /// </summary>
  166. /// <param name="text">The text of the button</param>
  167. /// <param name="val">The return value in case this button is clicked</param>
  168. public void AddButton(string text, string val)
  169. {
  170. if(text == null)
  171. throw new ArgumentNullException("text","Text of a button cannot be null");
  172. if(val == null)
  173. throw new ArgumentNullException("val","Value of a button cannot be null");
  174. MessageBoxExButton button = new MessageBoxExButton();
  175. button.Text = text;
  176. button.Value = val;
  177. AddButton(button);
  178. }
  179. /// <summary>
  180. /// Add a standard button to the message box
  181. /// </summary>
  182. /// <param name="buttons">The standard button to add</param>
  183. public void AddButton(MessageBoxExButtons button)
  184. {
  185. string buttonText = MessageBoxExManager.GetLocalizedString(button.ToString());
  186. if(buttonText == null)
  187. {
  188. buttonText = button.ToString();
  189. }
  190. string buttonVal = button.ToString();
  191. MessageBoxExButton btn = new MessageBoxExButton();
  192. btn.Text = buttonText;
  193. btn.Value = buttonVal;
  194. if(button == MessageBoxExButtons.Cancel)
  195. {
  196. btn.IsCancelButton = true;
  197. }
  198. AddButton(btn);
  199. }
  200. /// <summary>
  201. /// Add standard buttons to the message box.
  202. /// </summary>
  203. /// <param name="buttons">The standard buttons to add</param>
  204. public void AddButtons(MessageBoxButtons buttons)
  205. {
  206. switch(buttons)
  207. {
  208. case MessageBoxButtons.OK:
  209. AddButton(MessageBoxExButtons.Ok);
  210. break;
  211. case MessageBoxButtons.AbortRetryIgnore:
  212. AddButton(MessageBoxExButtons.Abort);
  213. AddButton(MessageBoxExButtons.Retry);
  214. AddButton(MessageBoxExButtons.Ignore);
  215. break;
  216. case MessageBoxButtons.OKCancel:
  217. AddButton(MessageBoxExButtons.Ok);
  218. AddButton(MessageBoxExButtons.Cancel);
  219. break;
  220. case MessageBoxButtons.RetryCancel:
  221. AddButton(MessageBoxExButtons.Retry);
  222. AddButton(MessageBoxExButtons.Cancel);
  223. break;
  224. case MessageBoxButtons.YesNo:
  225. AddButton(MessageBoxExButtons.Yes);
  226. AddButton(MessageBoxExButtons.No);
  227. break;
  228. case MessageBoxButtons.YesNoCancel:
  229. AddButton(MessageBoxExButtons.Yes);
  230. AddButton(MessageBoxExButtons.No);
  231. AddButton(MessageBoxExButtons.Cancel);
  232. break;
  233. }
  234. }
  235. #endregion
  236. #region Ctor
  237. /// <summary>
  238. /// Ctor is internal because this can only be created by MBManager
  239. /// </summary>
  240. internal MessageBoxEx()
  241. {
  242. }
  243. /// <summary>
  244. /// Called by the manager when it is disposed
  245. /// </summary>
  246. internal void Dispose()
  247. {
  248. if(_msgBox != null)
  249. {
  250. _msgBox.Dispose();
  251. }
  252. }
  253. #endregion
  254. }
  255. }