Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Spell Check with RichTextBox Control
|
Introduction This article discusses about a program that adds spell check functionality to a RichTextBox control.
Consider you have a project in which there is a form with RichTextBox control or simply create a project with a form having RichTextBox control.
Create a Class to hold the dictionary in a DataSet object and to maintain the ChangeAll word list in the HashTable object. An object of this class should be created to invoke a spell check form which is the second additional thing to be added to your application. The spell checker form has all the options of a normal spell check process - such as Ignore, IgnoreAll, Change, ChangeAll, Add to Dictionary.
I have used a access database to maintain the dictionary. There are two tables and one among them is used to store the user selected words by the option “Add to Dictionary” while execution of spell checker form.
Add to Project
Create a project and a form with RichTextBox control if you haven't done so.
1) Add a Class. The class will contain the following code
Imports System.Data.OleDb
Public Class RTBSpellChecker Public ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Dict.mdb" Public Conn As OleDbConnection Public Cmd As OleDbCommand Public Cmd2 As OleDbCommand Public Adp As OleDbDataAdapter Public Adp2 As OleDbDataAdapter Public CmB2 As OleDbCommandBuilder Public DicT As DataSet Public IgnoreAllList As New Hashtable Public ChangeAllList As New Hashtable
Sub New() Conn = New OleDbConnection(ConnStr) Conn.Open() Cmd = New OleDbCommand("Select * from Words", Conn) Cmd2 = New OleDbCommand("Select * from Custom", Conn) Adp = New OleDbDataAdapter(Cmd) Adp2 = New OleDbDataAdapter(Cmd2) CmB2 = New OleDbCommandBuilder(Adp2) DicT = New DataSet("Dict") Adp.Fill(DicT, "Words") Adp2.Fill(DicT, "Custom") Dim DC(0) As DataColumn DC(0) = DicT.Tables("Words").Columns(0) DicT.Tables("Words").PrimaryKey = DC Dim Dr As DataRow For Each Dr In DicT.Tables("Custom").Rows Dim Dr2 As DataRow Dr2 = DicT.Tables("Words").NewRow Dr2.Item(0) = Dr.Item(0) Try DicT.Tables("Words").Rows.Add(Dr2) Catch ex As Exception End Try Dr2 = Nothing Next End Sub End Class
2) Add a Windows form with the following properties
Name : frMSpellCheck FormBorderStyle : FixedToolWindow Size : as per your requirement Text : Spell Checker (whatever you want)
Add two TextBoxes to this form with names 'Word1' and 'Word2' (one for showing notfound word in dictionary and another for change with text).
Put 6 command buttons and the name of the buttons should be btnIgore, btnIgnoreAll, btnChange, btnChangeAll, btnAdd2Dict, btnCancel
The code below is a set of functions to be added
#Region "Variables" Private Rtbox As RichTextBox Private SChk As RTBSpellChecker Private CurWord As String, CurLoc As String Private StartLoc As Integer, EndLoc As Integer, TotalLoc As Integer, cl As Integer Private Ended As Boolean = False Private ScheckDiff As Integer Private msgvar As Integer #End Region
#Region "Private Functions" Private Function Initiate() cl = Rtbox.SelectionStart If Rtbox.SelectionLength = 0 Then StartLoc = 1 EndLoc = Rtbox.TextLength TotalLoc = Rtbox.SelectionLength Else StartLoc = Rtbox.SelectionStart + 1 EndLoc = Rtbox.SelectionStart + Rtbox.SelectionLength TotalLoc = Rtbox.SelectionLength End If CurLoc = StartLoc End Function
Private Function FindNextNotFound() Word2.Text = "" If Ended Then Rtbox.SelectionLength = 0 Rtbox.SelectionStart = cl msgvar += 1 If msgvar = 1 Then MsgBox("Spell Check has been Completed") Me.Dispose() Exit Function End If Word1.Text = "" While True If Word1.Text <> "" Then Exit While CurWord = FindCurWord() If Ended = True Then Exit While ChkWord(CurWord) End While If Ended And Word1.Text = "" Then Rtbox.SelectionLength = 0 Rtbox.SelectionStart = cl msgvar += 1 If msgvar = 1 Then MsgBox("Spell Check has been Completed") Me.Dispose() Exit Function End If End Function
Private Function ChkWord(ByVal Word As String) Dim S As String = "" Try S = SChk.ChangeAllList.Item(Word) Catch ex1 As System.NullReferenceException Catch ex2 As Exception End Try If S <> "" Then Word2.Text = S NotFound(Word) ChangeWord() FindNextNotFound() Exit Function End If If SChk.DicT.Tables("Words").Rows.Contains(Word) Then Word1.Text = "" Else If Len(Word) >= 3 And Word.Substring(Len(Word) - 1, 1).ToLower = "s" Then If SChk.DicT.Tables("Words").Rows.Contains(Word.Substring(0, Len(Word) - 1)) Then Word1.Text = "" Else NotFound(Word) End If Else NotFound(Word) End If End If End Function
Private Function NotFound(ByVal Word) Word1.Text = Word Rtbox.SelectionStart = CurLoc - Len(Word) - 2 Rtbox.SelectionLength = Len(Word) CurLoc = CurLoc + ScheckDiff End Function
Private Function ChangeWord() Rtbox.SelectedText = Word2.Text ScheckDiff = Len(Word2.Text) - Len(Word1.Text) CurLoc += ScheckDiff EndLoc += ScheckDiff ScheckDiff = 0 End Function
Private Function FindCurWord() As String If Ended Then Exit Function Dim C As Char, Word As String = "" While True C = Mid(Rtbox.Text, CurLoc, 1) If Char.IsLetter(Chr(Asc(C))) Then CurLoc += 1 If CurLoc >= EndLoc Then Ended = True : Exit While Word += C Else CurLoc += 1 If CurLoc >= EndLoc Then Ended = True : Exit While If Len(Word) > 0 Then Exit While End If End While Return Word End Function
Private Function AddToDictionary() Dim DR1 As DataRow, DR2 As DataRow DR1 = SChk.DicT.Tables("Words").NewRow DR2 = SChk.DicT.Tables("Custom").NewRow DR1.Item(0) = Word1.Text DR2.Item(0) = Word1.Text SChk.DicT.Tables("Words").Rows.Add(DR1) SChk.DicT.Tables("Custom").Rows.Add(DR2) SChk.Adp2.Update(SChk.DicT, "Custom") End Function
Private Function AddToIgnoreAllList() Dim dr As DataRow = SChk.DicT.Tables("Words").NewRow dr.Item(0) = Word1.Text SChk.DicT.Tables("Words").Rows.Add(dr) End Function
Private Function AddToIgnoreAllList(ByVal Word) Dim dr As DataRow = SChk.DicT.Tables("Words").NewRow dr.Item(0) = Word SChk.DicT.Tables("Words").Rows.Add(dr) End Function
Private Function AddToChangeAllList() Try SChk.ChangeAllList.Add(Word1.Text, Word2.Text) Catch ex As ArgumentException End Try End Function #End Region
#Region "Public Functions" Public Function setSP(ByRef RichTxtBox As RichTextBox, ByRef Sc As RTBSpellChecker) Rtbox = RichTxtBox SChk = Sc Rtbox.HideSelection = False End Function #End Region
Also add event handlers to all 6 buttons
Private Sub frMSpellCheck_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Initiate() FindNextNotFound() End Sub
Private Sub btnIgnore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIgnore.Click FindNextNotFound() End Sub
Private Sub btnIgnoreAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIgnoreAll.Click AddToIgnoreAllList() FindNextNotFound() End Sub
Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click ChangeWord() FindNextNotFound() End Sub
Private Sub butChangeAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butChangeAll.Click AddToChangeAllList() ChangeWord() FindNextNotFound() End Sub
Private Sub butCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCancel.Click Me.Dispose() End Sub
Private Sub btnAdd2Dict_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd2Dict.Click AddToDictionary() FindNextNotFound() End Sub
The dictioinary This spell checker uses a access database (mdb file) as its english dictionary. So you have to create a access database with the name "Dict.mdb" and should be stored in you project's bin directory i.e application path or exe file directory.
Create a table with name "Words" in which create a column with name "Word" and with type "Text".
Also create a table with name "Custom" in which create a column with name "Word" and with type "Text".
Add all the english words to the Words table. You don't need to add plural forms.
The custom dictionary will be updated at runtime with the option "Add to Dictionary".
If anyone wants the dictionary with morethan 100000 words, request mail to me for Access database.
Final Step
Go to the having RichTextBox. Add a button to invoke the Spell Checker with name "btnSpell" with the following code.
Public SChecker As RTBSpellChecker
Add the following in form load event
SChecker = New RTBSpellChecker
Add the following in click event of btnSpell button
Dim objfrmSpellCheck As New frMSpellCheck objfrmSpellCheck.setSP(RichTextBox1, SChecker) objfrmSpellCheck.ShowDialog(me)
|
Responses
|
| Author: Praveen Pula 13 Aug 2009 | Member Level: Gold Points : 1 | Hi it is a good article
if you have sample working code please update it hear.
|
|