Example #1
0
        private void ButtonClick(object sender, EventArgs e)
        {
            Button button = sender as Button;

            if (button != null)
            {
                // now you know the button that was clicked
                switch (button.Tag)
                {
                case ButtonType.Close:
                    this.Close();
                    break;

                case ButtonType.Yes:
                    mbResult = MessageBoxResults.Yes;
                    this.Close();
                    break;

                case ButtonType.No:
                    mbResult = MessageBoxResults.No;
                    this.Close();
                    break;
                }
            }
        }
Example #2
0
        /// <summary>Returns a message box result</summary>
        /// <param name="viewName">Name of the view (ignored if the view is not found).</param>
        /// <param name="messageBoxText">Message box text message (plain text)</param>
        /// <param name="caption">Message box caption (title)</param>
        /// <param name="buttons">Standard buttons displayed by the message box</param>
        /// <param name="icon">Standard icon displayed by the message box.</param>
        /// <param name="defaultResult">Default standard button</param>
        /// <param name="actions">Custom actions to be added to the message box as buttons. (Note: If this parameter is not null, the 'buttons' parameter is ignored)</param>
        /// <param name="model">Custom message box view model. (Note: Only used in exceptional scenarios where the standard view model .</param>
        /// <returns>Message box result</returns>
        protected virtual MessageBoxResult MessageBox(string viewName = "", string messageBoxText = "", string caption = "Message", MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxImages icon = MessageBoxImages.Information, MessageBoxResults defaultResult = MessageBoxResults.OK, IEnumerable<IViewAction> actions = null, MessageBoxViewModel model = null)
        {
            bool mustAddButtons = false;
            if (model == null)
            {
                model = new MessageBoxViewModel {Text = messageBoxText, Caption = caption, Icon = icon};
                mustAddButtons = true;
            }

            if (actions == null && mustAddButtons)
                switch (buttons)
                {
                    case MessageBoxButtons.OK:
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_OK, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.OK;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.OK), IsCancel = true});
                        break;
                    case MessageBoxButtons.OKCancel:
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_OK, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.OK;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.OK)});
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_Cancel, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.Cancel;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.Cancel), IsCancel = true});
                        break;
                    case MessageBoxButtons.YesNo:
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_Yes, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.Yes;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.OK || defaultResult == MessageBoxResults.Yes)});
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_No, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.No;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.No), IsCancel = true});
                        break;
                    case MessageBoxButtons.YesNoCancel:
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_Yes, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.Yes;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.OK || defaultResult == MessageBoxResults.Yes)});
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_No, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.No;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.No)});
                        model.Actions.Add(new ViewAction(Properties.Resources.MessageBox_Cancel, execute: (a, o) =>
                        {
                            model.Result = MessageBoxResults.Cancel;
                            CloseViewForModel(model);
                        }) {IsDefault = (defaultResult == MessageBoxResults.Cancel), IsCancel = true});
                        break;
                }
            else if (actions != null)
                foreach (var action in actions)
                    model.Actions.Add(action);

            foreach (var messageAction in model.Actions.OfType<MessageBoxViewAction>())
                messageAction.Model = model;

            return MessageBox(model, viewName);
        }
Example #3
0
 /// <summary>Queues a message box result that is to be automatically provided for the next message box request.</summary>
 /// <param name="result">The message box result.</param>
 /// <param name="customAction">An optional custom action that is to be executed after the result is set.</param>
 /// <note>This is a useful feature for testing. Results can be queued up ahead of time causing the message box to not be displayed but to immediately call onComplete with the specified result set.</note>
 public static void QueueMessageBoxResult(MessageBoxResults result, Action<MessageBoxViewModel> customAction = null)
 {
     if (_messageBoxResultQueue == null) _messageBoxResultQueue = new Queue<MessageBoxResultQueueItem>();
     lock (_messageBoxResultQueue)
         _messageBoxResultQueue.Enqueue(new MessageBoxResultQueueItem {Result = result, CustomAction = customAction});
 }
Example #4
0
 /// <summary>Returns a message box result</summary>
 /// <param name="messageBoxText">Message box text message (plain text)</param>
 /// <param name="caption">Message box caption (title)</param>
 /// <param name="buttons">Standard buttons displayed by the message box</param>
 /// <param name="icon">Standard icon displayed by the message box.</param>
 /// <param name="defaultResult">Default standard button</param>
 /// <returns>Message box result</returns>
 protected virtual MessageBoxResult MessageBox(string messageBoxText, string caption = "Message", MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxImages icon = MessageBoxImages.Information, MessageBoxResults defaultResult = MessageBoxResults.OK)
 {
     string viewName = "Index";
     if (RequestContext.RouteData.Data.ContainsKey("action"))
         viewName = RequestContext.RouteData.Data["action"].ToString();
     return MessageBox(viewName, messageBoxText, caption, buttons, icon, defaultResult);
 }
Example #5
0
        /// <summary>Displays a message box</summary>
        /// <param name="messageBoxText">Message box text message (plain text)</param>
        /// <param name="caption">Message box caption (title)</param>
        /// <param name="buttons">Standard buttons displayed by the message box</param>
        /// <param name="icon">Standard icon displayed by the message box.</param>
        /// <param name="defaultResult">Default standard button</param>
        /// <param name="onComplete">Code to run when the message box closes.</param>
        /// <param name="actions">Custom actions to be added to the message box as buttons. (Note: If this parameter is not null, the 'buttons' parameter is ignored)</param>
        /// <param name="model">Custom message box view model. (Note: Only used in exceptional scenarios where the standard view model .</param>
        /// <param name="viewName">Name of a custom view to be used by the message box (optional).</param>
        /// <param name="controllerType">Type of the controller (used as a context to find views)</param>
        public static void Message(string messageBoxText = "", string caption = "Message", MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxImages icon = MessageBoxImages.Information, MessageBoxResults defaultResult = MessageBoxResults.OK, Action<MessageBoxResult> onComplete = null, IEnumerable<IViewAction> actions = null, MessageBoxViewModel model = null, string viewName = "", Type controllerType = null)
        {
            var context = new RequestContext(new RouteData("MessageBox", new {viewName = string.Empty, messageBoxText, caption, buttons, icon, defaultResult}));

            // If a controller type was specified, we try to use it, which provides a context to find views
            Controller controller;
            if (controllerType == null) controller = new Controller();
            else controller = Activator.CreateInstance(controllerType) as Controller;
            if (controller == null) controller = new Controller();
            context.ProcessingController = controller;

            if (model != null && onComplete != null) model.OnComplete = onComplete; // Could be used later, especially for test scenarios where queued results simulate closing of message boxes

            bool mustHandle = _messageBoxResultQueue == null || _messageBoxResultQueue.Count == 0;

            context.Result = controller.MessageBox(viewName, messageBoxText, caption, buttons, icon, defaultResult, actions, model);
            var result = context.Result as MessageBoxResult;
            if (result != null && onComplete != null) result.ViewClosed += (o, e) => onComplete(result);

            if (mustHandle)
                // By default, we let view handlers handle the message box, unless simulated results are queued up.
                ExecuteViewHandlers(context);
        }