Step 3: Create a Customized Options Form
In this step, you will create an Options form that allows you to set spelling properties that will determine what occurs during a spell check. To create the Options form, complete the following steps:
1. Create a new Windows form.
• Go to the Solution Explorer and right-click the project.
• Select Add | New Item. The Add New Item window appears.
• Select Windows Form and rename the form OptionsForm.
• Click Add.
2. Resize the form.
3. Add the following basic controls to the form:
• Four CheckBox controls
• Four button controls
• One Label control
• One ListBox control
4. Set the following properties:
CheckBox controls:
CheckBox |
Property |
Setting |
1 |
Name |
cbSuggest |
|
Text |
Suggest corrections |
2 |
Name |
cbUpper |
|
Text |
Ignore words in upper case |
3 |
Name |
cbMixed |
|
Text |
Ignore words in mixed case |
4 |
Name |
cbNumbers |
|
Text |
Ignore words with numbers |
Label control:
Property |
Setting |
Text |
Custom dictionaries: |
ListBox control:
Property |
Setting |
Name |
lbFiles |
Button controls:
Button |
Property |
Setting |
1 |
Name |
btnOK |
|
Text |
OK |
2 |
Name |
btnCancel |
|
Text |
Cancel |
3 |
Name |
btnAdd |
|
Text |
Add |
4 |
Name |
btnRemove |
|
Text |
Remove |
These settings illustrate the following form:
To enable spell-checking functionality, you must enter code in the Code designer of the OptionsForm.
5. Select View | Code to open the OptionsForm Code designer if it is not already open.
6. To declare a private variable name for C1Spell and C1Spell.DlgSetting, enter the following code in the declaration section of the OptionsForm Code designer:
Private _spell As C1.Win.C1Spell.C1Spell
Private _finSettings As C1.Win.C1Spell.DlgSetting
• C#
private C1.Win.C1Spell.C1Spell _spell;
private C1.Win.C1Spell.DlgSetting _finSettings;
7. Enter the following code to connect to the C1Spell component to the OptionsForm:
Public Sub SetSpell(spell As C1.Win.C1Spell.C1Spell)
Me._spell = spell
End Sub
• C#
public void SetSpell(C1.Win.C1Spell.C1Spell spell)
{
this._spell = spell;
}
8. To implement the button controls on the OptionsForm (Remove, Add, and Cancel), enter the following code for the Button_Click event handler:
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
If lbFiles.SelectedIndex >= 0 Then
_finSettings.CustomDictionaries.RemoveAt(lbFiles.SelectedIndex)
lbFiles.Items.RemoveAt(lbFiles.SelectedIndex)
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim fd As New OpenFileDialog()
fd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim item As New C1.Win.C1Spell.DictionaryItem()
item.Checked = True
item.Item = fd.FileName
_finSettings.CustomDictionaries.Add(item)
lbFiles.Items.Add(item.Item)
End If
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Close()
End Sub
• C#
private void btnRemove_Click(object sender, EventArgs e)
{
if (lbFiles.SelectedIndex >= 0)
{
finSettings.CustomDictionaries.RemoveAt(lbFiles.SelectedIndex);
lbFiles.Items.RemoveAt(lbFiles.SelectedIndex);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
if (fd.ShowDialog() == DialogResult.OK)
{
C1.Win.C1Spell.DictionaryItem item = new C1.Win.C1Spell.DictionaryItem();
item.Checked = true;
item.Item = fd.FileName;
_finSettings.CustomDictionaries.Add(item);
lbFiles.Items.Add(item.Item);
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
9. To apply changes made to the C1Spell control if the OK button is clicked, enter the following code for the btnOK_Click event handler:
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
If Not _finSettings.Equals(_spell) Then
_finSettings.AssignTo(_spell)
If _spell.IsOptionSettingFixed Then
_spell.SaveSettings()
End If
End If
Close()
End Sub
• C#
private void btnOK_Click(object sender, EventArgs e)
{
if (!_finSettings.Equals(_spell))
{
_finSettings.AssignTo(_spell);
if (_spell.IsOptionSettingFixed)
spell.SaveSettings();
}
Close();
}
10. To implement the CheckBox controls on the OptionsForm, enter the following code in the CheckBox_CheckChanged event handler:
Private Sub cbSuggest_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbSuggest.CheckedChanged
_finSettings.HasSuggest = cbSuggest.Checked
End Sub
Private Sub cbUpper_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbUpper.CheckedChanged
_finSettings.IgnoreUpper = cbUpper.Checked
End Sub
Private Sub cbMixed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbMixed.CheckedChanged
_finSettings.IgnoreMixed = cbMixed.Checked
End Sub
Private Sub cbNumbers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbNumbers.CheckedChanged
_finSettings.IgnoreNumber = cbNumbers.Checked
End Sub
• C#
private void cbSuggest_CheckedChanged(object sender, EventArgs e)
{
_finSettings.HasSuggest = cbSuggest.Checked;
}
private void cbUpper_CheckedChanged(object sender, EventArgs e)
{
_finSettings.IgnoreUpper = cbUpper.Checked;
}
private void cbMixed_CheckedChanged(object sender, EventArgs e)
{
_finSettings.IgnoreMixed = cbMixed.Checked;
}
private void cbNumbers_CheckedChanged(object sender, EventArgs e)
{
_finSettings.IgnoreNumber = cbNumbers.Checked;
}
11. To implement the changes made to the CheckBox controls, add custom dictionaries, and load the settings, enter the following code:
Private Sub SetUI()
cbSuggest.Checked = _finSettings.HasSuggest
cbUpper.Checked = _finSettings.IgnoreUpper
cbNumbers.Checked = _finSettings.IgnoreNumber
cbMixed.Checked = _finSettings.IgnoreMixed
Dim item As C1.Win.C1Spell.DictionaryItem
For Each item In _finSettings.CustomDictionaries
lbFiles.Items.Add(item.Item)
Next item
End Sub
Private Sub OptionsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If _spell.IsOptionSettingFixed Then
_spell.LoadSettings()
End If
_finSettings = New C1.Win.C1Spell.DlgSetting(_spell)
SetUI()
End Sub
• C#
private void SetUI()
{
cbSuggest.Checked = _finSettings.HasSuggest;
cbUpper.Checked = _finSettings.IgnoreUpper;
cbNumbers.Checked = _finSettings.IgnoreNumber;
cbMixed.Checked = _finSettings.IgnoreMixed;
foreach(C1.Win.C1Spell.DictionaryItem item in _finSettings.CustomDictionaries)
lbFiles.Items.Add(item.Item);
}
private void OptionsForm_Load(object sender, EventArgs e)
{
if (_spell.IsOptionSettingFixed)
_spell.LoadSettings();
_finSettings = new C1.Win.C1Spell.DlgSetting(_spell);
SetUI();
}
|