// 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);
                }
            }
        }
        // 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);
                }
            }
        }
        // Cleans up data and structs from a single 
        // native dialog Show() invocation.
        private void CleanUp()
        {
            // Reset values that would be considered 
            // 'volatile' in a given instance.
            if (progressBar != null)
            {
                progressBar.Reset();
            }

            // Clean out sorted control lists - 
            // though we don't of course clear the main controls collection,
            // so the controls are still around; we'll 
            // resort on next show, since the collection may have changed.
            if (buttons != null) { buttons.Clear(); }
            if (commandLinks != null) { commandLinks.Clear(); }
            if (radioButtons != null) { radioButtons.Clear(); }
            progressBar = null;

            // Have the native dialog clean up the rest.
            if (nativeDialog != null)
            {
                nativeDialog.Dispose();
            }
        }