// Here we walk our controls collection and
        // sort the various controls by type.
        private void SortDialogControls()
        {
            foreach (TaskDialogControl control in controls)
            {
                TaskDialogButtonBase  buttonBase  = control as TaskDialogButtonBase;
                TaskDialogCommandLink commandLink = control as TaskDialogCommandLink;

                if (buttonBase != null && string.IsNullOrEmpty(buttonBase.Text) &&
                    commandLink != null && string.IsNullOrEmpty(commandLink.Instruction))
                {
                    throw new InvalidOperationException(LocalizedMessages.TaskDialogButtonTextEmpty);
                }

                TaskDialogRadioButton radButton;
                TaskDialogProgressBar progBar;

                // Loop through child controls
                // and sort the controls based on type.
                if (commandLink != null)
                {
                    commandLinks.Add(commandLink);
                }
                else if ((radButton = control as TaskDialogRadioButton) != null)
                {
                    if (radioButtons == null)
                    {
                        radioButtons = new List <TaskDialogButtonBase>();
                    }
                    radioButtons.Add(radButton);
                }
                else if (buttonBase != null)
                {
                    if (buttons == null)
                    {
                        buttons = new List <TaskDialogButtonBase>();
                    }
                    buttons.Add(buttonBase);
                }
                else if ((progBar = control as TaskDialogProgressBar) != null)
                {
                    progressBar = progBar;
                }
                else
                {
                    throw new InvalidOperationException(LocalizedMessages.TaskDialogUnkownControl);
                }
            }
        }
        // Gives event subscriber a chance to prevent
        // the dialog from closing, based on
        // the current state of the app and the button
        // used to commit. Note that we don't
        // have full access at this stage to
        // the full dialog state.
        internal int RaiseClosingEvent(int id)
        {
            EventHandler <TaskDialogClosingEventArgs> handler = Closing;

            if (handler != null)
            {
                TaskDialogButtonBase       customButton = null;
                TaskDialogClosingEventArgs e            = new TaskDialogClosingEventArgs();

                // Try to identify the button - is it a standard one?
                TaskDialogStandardButtons buttonClicked = MapButtonIdToStandardButton(id);

                // If not, it had better be a custom button...
                if (buttonClicked == TaskDialogStandardButtons.None)
                {
                    customButton = GetButtonForId(id);

                    // ... or we have a problem.
                    if (customButton == null)
                    {
                        throw new InvalidOperationException(LocalizedMessages.TaskDialogBadButtonId);
                    }

                    e.CustomButton     = customButton.Name;
                    e.TaskDialogResult = TaskDialogResult.CustomButtonClicked;
                }
                else
                {
                    e.TaskDialogResult = (TaskDialogResult)buttonClicked;
                }

                // Raise the event and determine how to proceed.
                handler(this, e);
                if (e.Cancel)
                {
                    return((int)HResult.False);
                }
            }

            // It's okay to let the dialog close.
            return((int)HResult.Ok);
        }