There are two ways to use the spell checkers in an MDI application, without adding burden to the system memory;
[In the following, a RapidSpellAYTDialogCoupler can also be used to couple the RapidSpellDialog and RapidSpellAsYouType controls.]
1. The simplest way is to use an instance of RapidSpellAsYouType and/or RapidSpellDialog on each child form. To avoid multiple instances of the dictionary we can set RapidSpellAsYouType.RapidSpellChecker.ShareDictionary = true (this must be done BEFORE the TextComponents property is set in that control).
The impacts of this approach are;
i. sharing the dictionary means we can only have one type of main dictionary loaded for the entire application - eg. you can't have a medical dictionary used on one form and an English dictionary on another.
ii. the dialog spell checker will run over the current child form only (which may be desirable).
2. An alternative approach is to place RapidSpellDialog and RapidSpellAsYouType on the MDI parent form, and to assign their text component properties as child forms are created/disposed.
The impacts of this are;
i. implicit in this approach is that the same dictionary will be used for all forms, so this is similar to method 1, but for a different reason.
ii. the dialog spell checker will work over all child forms, instead of the current one (although this can be changed as desired).
To implement this method do the following;
A. Add this class to the project
public class RapidSpellMDIManager
{
List<Keyoti.RapidSpell.IAYTTextBox> spellBoxes = new List<Keyoti.RapidSpell.IAYTTextBox>();
List<Form> childForms = new List<Form>();
Keyoti.RapidSpell. RapidSpellAsYouType ayt;
Keyoti.RapidSpell. RapidSpellDialog dialog;
public RapidSpellMDIManager(Keyoti.RapidSpell.RapidSpellAsYouType ayt, Keyoti.RapidSpell.RapidSpellDialog dialog)
{
this.ayt = ayt;
this.dialog = dialog;
}
public void RegisterChildForm(Form childForm)
{
childForms.Add(childForm);
AddChildTextBoxes(spellBoxes, childForm);
ayt.TextComponents = spellBoxes.ToArray();
dialog.TextBoxBasesToCheck = ConvertToTextBoxBase(spellBoxes.ToArray());
childForm.Disposed += new EventHandler(childForm_Disposed);
}
void childForm_Disposed(object sender, EventArgs e)
{
(sender as Form).Disposed -= new EventHandler(childForm_Disposed);
childForms.Remove(sender as Form);
spellBoxes.Clear();
foreach (Form childForm in childForms)
AddChildTextBoxes(spellBoxes, childForm);
ayt.TextComponents = null;
dialog.TextBoxBasesToCheck = null;
if (spellBoxes.Count > 0)
{
ayt.TextComponents = spellBoxes.ToArray();
dialog.TextBoxBasesToCheck = ConvertToTextBoxBase(spellBoxes.ToArray());
}
}
void AddChildTextBoxes(List<Keyoti.RapidSpell.IAYTTextBox> boxes, Control textComponentsUnder)
{
foreach (Control c in textComponentsUnder.Controls)
{
if (c is Keyoti.RapidSpell.IAYTTextBox)
boxes.Add(c as Keyoti.RapidSpell.IAYTTextBox);
else
AddChildTextBoxes(boxes, c);
}
}
TextBoxBase[] ConvertToTextBoxBase(Keyoti.RapidSpell.IAYTTextBox[] array)
{
List<TextBoxBase> bbases = new List<TextBoxBase>();
foreach (Keyoti.RapidSpell.IAYTTextBox box in array)
if (box is TextBoxBase) bbases.Add(box as TextBoxBase);
return bbases.ToArray();
}
}
B. Declare as a class member variable, and instantiate in the MDI parent's constructor
RapidSpellMDIManager spellMDIManager;
public MDIParent1()
{
InitializeComponent();
spellMDIManager = new RapidSpellMDIManager(rapidSpellAsYouType1, rapidSpellDialog1);
}
Where rapidSpellAsYouType1 and rapidSpellDialog1 are controls on the MDI parent form.
C. When adding a child form, register it with RapidSpellMDIManager
spellMDIManager.RegisterChildForm(childForm);
You can see that the RapidSpellMDIManager class simply keeps a list of text boxes and child forms, and uses those lists to set the text component properties in the spell checker controls. If the RapidSpellDialog, for example is not needed, just remove it from the RapidSpellMDIManager class. |