c# - 帮助找到最深的拼写检查子控件

标签 c# reflection controls refactoring parent-child

我正在尝试重构这段代码,因为它在我的程序中令人作呕 重复出现。

我的问题与这样一个事实有关,即在任何给定页面(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 上直接有一些控件,然后有两个 Group Boxes 也有控件。

我的部分目标是只检查可能需要拼写检查的控件,例如文本框及其相关项。

我知道我可以使用 control.HasChildren(),但我不知道如何使用它以及告诉我要走多深。我会假设两个级别是我曾经去过的最深的级别,但这似乎是短视的硬编码。

理想情况下,我会弄清楚如何将控件传递给我的 CheckSpelling(),然后在其中使用逻辑来确定要走多远。可能使用反射

这里的完整性是 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;
    }

最佳答案

我会采取稍微不同的方法。我会创建一个接口(interface),比方说 ISpellCheckable,它有一个名为 SpellCheck 的方法。

然后,我将扩展 TextBox 控件以创建一个新的 SpellCheckedTextBox,它实现了 ISpellCheckable。

然后,将页面上的相关文本框替换为 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);
            }
        }

关于c# - 帮助找到最深的拼写检查子控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1503992/

相关文章:

c# - 使用反射从父类获取自己的属性名称

php - 在 PHP 中打印没有嵌套循环的模式

android - Corona SDK 允许设备音量控制

c# - Entity Framework 核心缺乏适度的 LINQ 查询支持的最佳解决方案是什么?

c# - WebClient.DownloadFileTaskAsync() 实际上永远不会超时吗?

c# - 将 C# 委托(delegate)的调用约定更改为 CDECL

c# - 在 C# 中定义服务特定枚举的位置?

php - 什么是反射类?

c# - 强制绑定(bind)到数据源的复选框在尚未查看时更新

c# - 在 C# 中使用反射进行转换