Step 5: Provide Spell Checking for Arbitrary Strings (Bottom Panel)
This part of the demo allows the user to change the contents of a list box and to spell check each item by clicking a button. In this case, the C1Spell component is not connected to any other controls. All spell checking is done through its CheckString method.
First, add the following two routines to connect the TextBox5 and ListBox1 controls:
Private Sub ListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
If (ListBox1.SelectedIndex > -1) Then
TextBox5.Text = ListBox1.Items(ListBox1.SelectedIndex)
End If
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
If (ListBox1.SelectedIndex > -1) Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox5.Text
End If
End Sub
• C#
private void listBox1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1 )
{
textBox5.Text = (string)listBox1.Items[ListBox1.SelectedIndex];
}
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > -1)
{
listBox1.Items[listBox1.SelectedIndex] = textBox5.Text;
}
}
The first routine copies a selected item from ListBox1 into TextBox5. The second copies the contents of TextBox5 into ListBox1 when the user makes any changes.
The final step is the routine that does the spell checking. It simply scans the ListBox1 control checking each list item and setting its selected state to True if no spelling errors were found, or False otherwise. Here is the code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
' Check the listbox items one by one
For i = 0 To ListBox1.Items.Count - 1
If Not C1Spell3.CheckString(ListBox1.Items(i)) Then
' there are bad words in this item, so select the bad item
ListBox1.SetSelected(i, True)
' find any one, then exit for
Exit For
End If
Next
End Sub
• C#
private void button2_Click(object sender, EventArgs e)
{
int i;
// Check the listbox items one by one
for (i = 0; i <= listBox1.Items.Count – 1; i++)
{
if (!C1Spell3.CheckString(ListBox1.Items[i]) )
// there are bad words in this item, so select the bad item
listBox1.SetSelected(i, true);
// find any one, then exit for
break ;
}
}
To spell check in this panel, type a word into the text box. You will notice that the word displays in the list. If you click on another word in the list and click the Spell Check button the misspelled word will be highlighted in the list.
That's it. This demo covers most aspects of the C1Spell component.
|