using System; using System.Drawing; using System.Windows.Forms; namespace Utils.MessageBoxExLib { /// /// An extended MessageBox with lot of customizing capabilities. /// public class MessageBoxEx { #region Fields private MessageBoxExForm _msgBox = new MessageBoxExForm(); private bool _useSavedResponse = true; private string _name = null; #endregion #region Properties internal string Name { get{ return _name; } set{ _name = value; } } /// /// Sets the caption of the message box /// public string Caption { set{_msgBox.Caption = value;} } /// /// Sets the text of the message box /// public string Text { set{_msgBox.Message = value;} } /// /// Sets the icon to show in the message box /// public Icon CustomIcon { set{_msgBox.CustomIcon = value;} } /// /// Sets the icon to show in the message box /// public MessageBoxExIcon Icon { set{ _msgBox.StandardIcon = (MessageBoxIcon)Enum.Parse(typeof(MessageBoxIcon), value.ToString());} } /// /// Sets the font for the text of the message box /// public Font Font { set{_msgBox.Font = value;} } /// /// Sets or Gets the ability of the user to save his/her response /// public bool AllowSaveResponse { get{ return _msgBox.AllowSaveResponse; } set{ _msgBox.AllowSaveResponse = value; } } /// /// Sets the text to show to the user when saving his/her response /// public string SaveResponseText { set{_msgBox.SaveResponseText = value; } } /// /// Sets or Gets wether the saved response if available should be used /// public bool UseSavedResponse { get{ return _useSavedResponse; } set{ _useSavedResponse = value; } } /// /// Sets or Gets wether an alert sound is played while showing the message box. /// The sound played depends on the the Icon selected for the message box /// public bool PlayAlsertSound { get{ return _msgBox.PlayAlertSound; } set{ _msgBox.PlayAlertSound = value; } } /// /// Sets or Gets the time in milliseconds for which the message box is displayed. /// public int Timeout { get{ return _msgBox.Timeout; } set{ _msgBox.Timeout = value; } } /// /// Controls the result that will be returned when the message box times out. /// public TimeoutResult TimeoutResult { get{ return _msgBox.TimeoutResult; } set{ _msgBox.TimeoutResult = value; } } #endregion #region Methods /// /// Shows the message box /// /// public string Show() { return Show(null); } /// /// Shows the messsage box with the specified owner /// /// /// public string Show(IWin32Window owner) { if(_useSavedResponse && this.Name != null) { string savedResponse = MessageBoxExManager.GetSavedResponse(this); if( savedResponse != null) return savedResponse; } if(owner == null) { _msgBox.ShowDialog(); } else { _msgBox.ShowDialog(owner); } if(this.Name != null) { if(_msgBox.AllowSaveResponse && _msgBox.SaveResponse) MessageBoxExManager.SetSavedResponse(this, _msgBox.Result); else MessageBoxExManager.ResetSavedResponse(this.Name); } else { Dispose(); } return _msgBox.Result; } /// /// Add a custom button to the message box /// /// The button to add public void AddButton(MessageBoxExButton button) { if(button == null) throw new ArgumentNullException("button","A null button cannot be added"); _msgBox.Buttons.Add(button); if(button.IsCancelButton) { _msgBox.CustomCancelButton = button; } } /// /// Add a custom button to the message box /// /// The text of the button /// The return value in case this button is clicked public void AddButton(string text, string val) { if(text == null) throw new ArgumentNullException("text","Text of a button cannot be null"); if(val == null) throw new ArgumentNullException("val","Value of a button cannot be null"); MessageBoxExButton button = new MessageBoxExButton(); button.Text = text; button.Value = val; AddButton(button); } /// /// Add a standard button to the message box /// /// The standard button to add public void AddButton(MessageBoxExButtons button) { string buttonText = MessageBoxExManager.GetLocalizedString(button.ToString()); if(buttonText == null) { buttonText = button.ToString(); } string buttonVal = button.ToString(); MessageBoxExButton btn = new MessageBoxExButton(); btn.Text = buttonText; btn.Value = buttonVal; if(button == MessageBoxExButtons.Cancel) { btn.IsCancelButton = true; } AddButton(btn); } /// /// Add standard buttons to the message box. /// /// The standard buttons to add public void AddButtons(MessageBoxButtons buttons) { switch(buttons) { case MessageBoxButtons.OK: AddButton(MessageBoxExButtons.Ok); break; case MessageBoxButtons.AbortRetryIgnore: AddButton(MessageBoxExButtons.Abort); AddButton(MessageBoxExButtons.Retry); AddButton(MessageBoxExButtons.Ignore); break; case MessageBoxButtons.OKCancel: AddButton(MessageBoxExButtons.Ok); AddButton(MessageBoxExButtons.Cancel); break; case MessageBoxButtons.RetryCancel: AddButton(MessageBoxExButtons.Retry); AddButton(MessageBoxExButtons.Cancel); break; case MessageBoxButtons.YesNo: AddButton(MessageBoxExButtons.Yes); AddButton(MessageBoxExButtons.No); break; case MessageBoxButtons.YesNoCancel: AddButton(MessageBoxExButtons.Yes); AddButton(MessageBoxExButtons.No); AddButton(MessageBoxExButtons.Cancel); break; } } #endregion #region Ctor /// /// Ctor is internal because this can only be created by MBManager /// internal MessageBoxEx() { } /// /// Called by the manager when it is disposed /// internal void Dispose() { if(_msgBox != null) { _msgBox.Dispose(); } } #endregion } }