/// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handles the Click event of the btnNew control.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void btnNew_Click(object sender, EventArgs e)
        {
            int i = 1;
            var newSelectionRuleNameTemplate = LocalizationManager.GetString(
                "RenderingSelectionRulesDlg.NewSelectionRuleNameTemplate",
                "Selection Rule - {0}", "Param is an integer");
            string name = Format(newSelectionRuleNameTemplate, i);

            Func <string, bool> nameIsUnique = n => !m_listRules.Items.Cast <RenderingSelectionRule>().Any(r => r.Name == n);

            while (!nameIsUnique(name))
            {
                name = Format(newSelectionRuleNameTemplate, ++i);
            }

            RenderingSelectionRule rule = new RenderingSelectionRule(name);

            using (RulesWizardDlg dlg = new RulesWizardDlg(rule, true, Word.AllWords, m_selectKeyboard, nameIsUnique))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    m_listRules.SelectedIndex = m_listRules.Items.Add(rule);
                    if (rule.Valid)
                    {
                        m_listRules.SetItemChecked(m_listRules.SelectedIndex, true);
                    }
                }
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handles the Click event of the btnCopy control.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void btnCopy_Click(object sender, EventArgs e)
        {
            int iOrigRule = m_listRules.SelectedIndex;
            RenderingSelectionRule origRule = m_listRules.SelectedItem as RenderingSelectionRule;
            RenderingSelectionRule newRule  = new RenderingSelectionRule(origRule.QuestionMatchingPattern, origRule.RenderingMatchingPattern);

            var copiedSelectionRuleNameTemplate = LocalizationManager.GetString("RenderingSelectionRulesDlg.CopiedSelectionRuleNameTemplate",
                                                                                "{0} - Copy{1}", "Param 0: the original rule name; Param 1: an optional numeric suffix to prevent duplicates (if needed)");
            int i    = 1;
            var name = Format(copiedSelectionRuleNameTemplate, origRule.Name, string.Empty);

            Func <string, bool> nameIsUnique = n => !m_listRules.Items.Cast <RenderingSelectionRule>().Any(r => r.Name == n);

            while (!nameIsUnique(name))
            {
                name = Format(copiedSelectionRuleNameTemplate, origRule.Name, "(" + i++ + ")");
            }

            newRule.Name = name;

            using (RulesWizardDlg dlg = new RulesWizardDlg(newRule, true, Word.AllWords, m_selectKeyboard, nameIsUnique))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    m_listRules.SelectedIndex = m_listRules.Items.Add(newRule);
                    if (newRule.Valid)
                    {
                        m_listRules.SetItemChecked(m_listRules.SelectedIndex, m_listRules.GetItemChecked(iOrigRule));
                    }
                }
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handles the Click event of the btnNew control.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void btnNew_Click(object sender, EventArgs e)
        {
            int    i    = 1;
            string name = string.Format(Properties.Resources.kstidNewSelectionRuleNameTemplate, i);

            Func <string, bool> nameIsUnique = n => !m_listRules.Items.Cast <RenderingSelectionRule>().Any(r => r.Name == n);

            while (!nameIsUnique(name))
            {
                name = string.Format(Properties.Resources.kstidNewSelectionRuleNameTemplate, ++i);
            }

            RenderingSelectionRule rule = new RenderingSelectionRule(name);

            using (RulesWizardDlg dlg = new RulesWizardDlg(rule, Word.AllWords, m_selectKeyboard, nameIsUnique))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    m_listRules.SelectedIndex = m_listRules.Items.Add(rule);
                    if (rule.Valid)
                    {
                        m_listRules.SetItemChecked(m_listRules.SelectedIndex, true);
                    }
                }
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handles the Click event of the btnEdit control.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void btnEdit_Click(object sender, EventArgs e)
        {
            RenderingSelectionRule rule = m_listRules.SelectedItem as RenderingSelectionRule;
            string origName             = rule.Name;
            string origQ = rule.QuestionMatchingPattern;
            string origR = rule.RenderingMatchingPattern;
            Func <string, bool> nameIsUnique = n => !m_listRules.Items.Cast <RenderingSelectionRule>().Where(r => r != rule).Any(r => r.Name == n);

            using (RulesWizardDlg dlg = new RulesWizardDlg(rule, false, Word.AllWords, m_selectKeyboard, nameIsUnique))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    if (!rule.Valid)
                    {
                        m_listRules.SetItemChecked(m_listRules.SelectedIndex, false);
                    }

                    m_listRules.Invalidate();
                }
                else
                {
                    rule.Name = origName;
                    rule.QuestionMatchingPattern  = origQ;
                    rule.RenderingMatchingPattern = origR;
                }
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handles the Click event of the btnCopy control.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private void btnCopy_Click(object sender, EventArgs e)
        {
            int iOrigRule = m_listRules.SelectedIndex;
            RenderingSelectionRule origRule = m_listRules.SelectedItem as RenderingSelectionRule;
            RenderingSelectionRule newRule  = new RenderingSelectionRule(origRule.QuestionMatchingPattern, origRule.RenderingMatchingPattern);

            int    i    = 1;
            string name = string.Format(Properties.Resources.kstidCopiedSelectionRuleNameTemplate, origRule.Name, string.Empty);

            Func <string, bool> nameIsUnique = n => !m_listRules.Items.Cast <RenderingSelectionRule>().Any(r => r.Name == n);

            while (!nameIsUnique(name))
            {
                name = string.Format(Properties.Resources.kstidCopiedSelectionRuleNameTemplate, origRule.Name, "(" + i++ + ")");
            }

            newRule.Name = name;

            using (RulesWizardDlg dlg = new RulesWizardDlg(newRule, Word.AllWords, m_selectKeyboard, nameIsUnique))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    m_listRules.SelectedIndex = m_listRules.Items.Add(newRule);
                    if (newRule.Valid)
                    {
                        m_listRules.SetItemChecked(m_listRules.SelectedIndex, m_listRules.GetItemChecked(iOrigRule));
                    }
                }
            }
        }