I am trying to Refactor this code as it is repeated ad nauseum throughout my program.
My problem has to do with the fact that on any given page(tabpage,panel,uc,etc) there are controls at multiple levels to spellcheck.
i.e. -->
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();
}
}
}
As you can see in the example, tpgSystems
has some controls directly on it and then there are two Group Boxes
that have controls in them as well.
Part of my goal in this was to only check controls that had a possibility of needing Spell Checking, as in Text Boxes
and there relatives.
I know there is the control.HasChildren()
that I can use but what is escaping me is how to use that and tell how deep to go. I would assume that two levels is the deepest I would ever go but that seems short sighted to hard code that in.
Ideally I would figure out how to pass a control to my CheckSpelling()
and then have the logic in there to figure out how deep to go. Probably using Reflection.
For completeness here is CheckSpelling()
which is in a seperate library I created.
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;
}