1

このコードはプログラム全体で繰り返されるため、このコードをリファクタリングしようとしています

私の問題は、特定のページ ( tabpage、panel、uc など) に複数のレベルでスペルチェックするコントロールがあるという事実に関係しています。
つまり -->

            foreach (Control control in tpgSystems.Controls)
            {
                if (control.GetType() == typeof(MemoExEdit))
                {
                    if (control.Text != String.Empty)
                    {
                        control.BackColor = Color.FromArgb(180, 215, 195);
                        control.Text = HUD.Spelling.CheckSpelling(control.Text);
                        control.ResetBackColor();
                    }
                }
            }
            foreach (Control control in grpCogestiveHeartFailure.Controls)
            {
                if (control.GetType() == typeof(MemoExEdit))
                {
                    if (control.Text != String.Empty)
                    {
                        control.BackColor = Color.FromArgb(180, 215, 195);
                        control.Text = HUD.Spelling.CheckSpelling(control.Text);
                        control.ResetBackColor();
                    }
                }
            }
            foreach (Control control in grpDiabetes.Controls)
            {
                if (control.GetType() == typeof(MemoExEdit))
                {
                    if (control.Text != String.Empty)
                    {
                        control.BackColor = Color.FromArgb(180, 215, 195);
                        control.Text = HUD.Spelling.CheckSpelling(control.Text);
                        control.ResetBackColor();
                    }
                }
            }

例でわかるように、 にtpgSystemsはいくつかのコントロールが直接あり、さらに 2 つのGroup Boxesコントロールも含まれています。

これにおける私の目標の一部は、スペル チェックが必要になる可能性のあるコントロールのみをチェックすることでしたText Boxes

使用できるものがあることは知っていますが、control.HasChildren()それをどのように使用し、どれだけ深く行くかを教えてくれることから逃れています。2 つのレベルがこれまでで最も深いものだと思いますが、それをハードコーディングするには近視眼的です。

理想的には、コントロールを my に渡す方法を見つけてCheckSpelling()、そこにロジックを入れて、どのくらい深くするかを決定します。おそらくReflectionを使用しています。

完全CheckSpelling()を期すために、これは私が作成した別のライブラリにあります。

public string CheckSpelling(string text)
    {
        Word.Application app = new Word.Application();
        object nullobj = Missing.Value;
        object template = Missing.Value;
        object newTemplate = Missing.Value;
        object documentType = Missing.Value;
        object visible = false;
        object optional = Missing.Value;
        object savechanges = false;
        app.ShowMe();

        Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);

        doc.Words.First.InsertBefore(text);
        Word.ProofreadingErrors errors = doc.SpellingErrors;

        var ecount = errors.Count;
        doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional, 
            ref optional, ref optional, ref optional, ref optional, ref optional, 
            ref optional, ref optional, ref optional);
        object first = 0;
        object last = doc.Characters.Count - 1;
        var results = doc.Range(ref first, ref last).Text;
        doc.Close(ref savechanges, ref nullobj, ref nullobj);
        app.Quit(ref savechanges, ref nullobj, ref nullobj);

        return results;
    }
4

3 に答える 3

3

私は少し異なるアプローチを取るでしょう。SpellCheck と呼ばれる単一のメソッドを持つ ISpellCheckable としましょう。

次に、TextBox コントロールを拡張して、ISpellCheckable を実装する新しい SpellCheckedTextBox を作成します。

次に、ページ上の関連する TextBoxes を SpellCheckedTextBox に置き換えます (単純な変更)。

次に、ページ上のすべてのコントロールを再帰的に下降し、それらが ISpellCheckable を実装しているかどうかをチェックし、実装している場合は SpellCheck メソッドを呼び出す単一のメソッドを単純に記述できます。

void WalkControls(Control root)
        {
            if (root == null ) return;
            foreach (Control control in root.Controls)
            {
                if (control is ISpellCheckable)
                {
                    if (((ISpellCheckable)control).SpellCheck())
                    {
                        // do stuff
                    }
                }
                WalkControls(control);
            }
        }
于 2009-10-01T13:38:46.570 に答える