Dialogs and Modality

The latest version introduces a new type of control, the Dialog class.
Dialogs are Windows that provide a way to dispatch a DialogResult.

Take a look at the source code:

 public abstract class Dialog : Window
    {
        private DialogResult _dialogResult;

        public delegate void DialogResultEventHandler(Dialog sender, DialogResult result);
        public event DialogResultEventHandler OnDialogResult;

        protected DialogResult DialogResult
        {
            get
            {
                return _dialogResult;
            }
            set
            {
                if (_dialogResult == value) return;

                _dialogResult = value;

                if (OnDialogResult != null)
                    OnDialogResult(this, _dialogResult);
            }
        }

        public override void Show(Desktop target)
        {
            _dialogResult = DialogResult.None;
            base.Show(target);
        }
    }

As you can see, it’s a rather naked class, but the event is valuable once you start using modal dialogs, such as confirmation dialogs, notification popups, etc.

For obvious reasons, we cannot stall the game loop in order to wait for user input.
Instead we use a straight forward delegate to catch the dialog result.
Lets take a look at  how you make use of this.

We start by writing our own little Dialog:

    public class MyDialog : Dialog
    {
        protected override void Initialize()
        {
            // set some default size
            Size = new Point(200, 100);

            Button buttonCancel = new Button();
            buttonCancel.Text = "Cancel";
            buttonCancel.Size = new Point(200, 35);
            buttonCancel.Dock = DockStyle.Bottom;
            Controls.Add(buttonCancel);

            Button buttonOK = new Button();
            buttonOK.Text = "OK";
            buttonOK.Size = new Point(200, 35);
            buttonOK.Dock = DockStyle.Bottom;
            Controls.Add(buttonOK);

            // set the dialog result as needed
            buttonCancel.OnMouseClick += delegate(Control sender) { DialogResult = DialogResult.Cancel; };
            buttonOK.OnMouseClick += delegate(Control sender) { DialogResult = DialogResult.OK; };
        }
    }

Phew, that was straight forward.

And here is how we open the dialog and handle possible results :

        MyDialog dialog = new MyDialog();
        dialog.Modal = true;
        // handle the result
        dialog.OnDialogResult+=new Dialog.DialogResultEventHandler(dialog_OnDialogResult);
        dialog.Show(this);

        void dialog_OnDialogResult(Dialog sender, DialogResult result)
        {
            // do something
        }

And that’s it already!
Note that the Dialog will have to be closed in order to access the rest of the GUI again:

        void dialog_OnDialogResult(Dialog sender, DialogResult result)
        {
            // do something with result
            // and close it
            sender.Close();
        }

You can open as many modal dialogs as you like, and they have to be closed in order of appearance. Exactly like you’re used to from your operating system.

Take a good look at the MessageBox class in the latest version of the demo application. It shows how to inherit Dialog and create a resemblence of the  Windows.Forms.MessageBox functionality.

Just grab the latest version and have fun!

Posted in SQUID

Leave a Reply

Your email address will not be published.