Textbox control in vbnet which will have red underline if wrong spelling and make a right click to show correct spelling

  • Thread starter Thread starter jepoyman
  • Start date Start date
J

jepoyman

Guest
Good day,

First of all I would like to greet everyone a nice and Blessed day!.

I am a newbie in programming actually I am doing it for a hobby. I am creating a simple data base application with access data base for my wife.

I placed a textbox which enabled the multi line on for me make it big and on properties made it scroll vertical in case the entire textbox is getting filled user can scroll up and down the entire text in the box.

I would like to implement whenever a user is typing a word which is wrong spelling there would be a curly red red underline, blue or green curly lines on it , it would be like on microsoft word then the user will right click on the wrong spelling word and will appear selections of words with correct spelling to choose from which will replace the wrong word.

I have been dealing with this for almost a week now giving it at least 2 hour everyday as a hobby.

I saw some instructions from other website "" DevCity.NET :: Adding Spelling and Grammar Checking Functions into VB.NET Applications"" and downloaded the spellcheck.zip sample projects which was created on much older version of visual studio. Upon opening the project my Visual Studio 2015 converts it.

I also created a virtual machine windows xp installed with Visual Studio 2008 to see if i will be able to have the details of the form however no luck.

It gave me some errors, which confuses me a lot. From the solution explorer are Form1 and SpellCheckTextbox.vb. On the SpellCheckTextbox.vb lies the codes in it there is also a

Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

I tried to replicate it which created a new form and copy pasted the codes one-by-one and study it what went wrong. But I couldnt get it right.

I already referenced and added the microsoft word dll on the project.

Can anyone point me out in the right direction because the more I replicate it the more I am getting confused.

It would be a big help as well if there will be someone to correct the codes or create a whole new codes for this.

Region "Imports"

Imports Microsoft.Office
Imports System.Reflection
Imports Microsoft.Office.Interop


#End Region
#Region "Profile"
'Name:Dipak Fatania
'Contact:dipakfatania@hotmail.com

#End Region
Public Class SpellCheckTextBox
Inherits RichTextBox

#Region " InitializeControl Implementation "

#End Region

#Region "Variables"

Dim m_blnSpellCheck As Boolean = True
Dim m_app As Word.Application = New Word.Application()
Friend WithEvents SpellCheckTextBox1 As TextBox
Dim m_doc As Word.Document = m_app.Documents.Add()


#End Region

#Region "Constructor"

Public Sub New()
MyBase.New()
End Sub

#End Region

#Region "Public Property"

Public Property SpellCheck() As Boolean
Get
Return m_blnSpellCheck
End Get
Set(ByVal value As Boolean)
m_blnSpellCheck = value
End Set
End Property

#End Region

#Region "Eventes"

Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim m_item As ToolStripMenuItem = CType(sender, ToolStripMenuItem)

Dim m_pointArray As Int16() = CType(m_item.Tag, Int16())

Dim m_strFirstPart As String = String.Empty

If (m_pointArray(0) > 0) Then
m_strFirstPart = Me.Text.Substring(0, m_pointArray(0)) + " "
End If

Dim m_strMiddlePart As String = Me.Text.Substring(m_pointArray(0) + 1, m_pointArray(1) - m_pointArray(0))

Dim m_strLastPart As String = Me.Text.Substring(m_pointArray(1) + 1)

Me.SelectionStart = m_pointArray(0) + 1

Me.SelectionLength = m_strMiddlePart.Length

Me.SelectionFont = New Font(Me.SelectionFont.FontFamily, Me.SelectionFont.Size, FontStyle.Regular)

Me.SelectedText = m_item.Text + " "

Me.Refresh()


End Sub

Private Sub SpellCheckTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp


If (Me.Text.Length = 0) Then
Me.SelectionFont = New Font(Me.SelectionFont.FontFamily, Me.SelectionFont.Size, FontStyle.Regular)
Me.Refresh()
End If

If (e.KeyValue = 32) Then
Dim m_strRecentWord As String = String.Empty
Dim m_intLastIndex As Integer = Me.Text.Substring(0, Me.SelectionStart - 1).LastIndexOf(" ")
Dim m_intLength As Integer = Me.SelectionStart - m_intLastIndex - 2
m_strRecentWord = Me.Text.Substring(m_intLastIndex + 1, m_intLength)
m_strRecentWord = m_strRecentWord.Trim()

If (m_strRecentWord.Length > 0 And IsWrongWord(m_strRecentWord) = True) Then
Me.SelectionStart = m_intLastIndex + 1
Me.SelectionLength = m_intLength
Me.SelectionFont = New Font(Me.SelectionFont.FontFamily, Me.SelectionFont.Size, FontStyle.Underline)
Me.SelectionStart = Me.SelectionStart + Me.SelectionLength + 1
Me.Refresh()
End If

End If
End Sub

Private Sub SpellCheckTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
UnderLineWrongWords()
Call SpellChecking()
End Sub

Private Sub SpellCheckTextBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'If (Me.SpellCheck = False) Then
If (e.Button = Windows.Forms.MouseButtons.Right) Then
Dim m_objContextMenuStrip As New ContextMenuStrip

'get string from starting to clicked position
Dim m_intClickIndex As Int16 = Me.GetCharIndexFromPosition(New Point(e.X, e.Y))

'index of clicked char
Dim m_strInitialString As String = Me.Text.Substring(0, m_intClickIndex)

'initialise index upto total lengh in case we are clicking on last word
Dim m_intStartIndex As Int16 = Me.Text.Length - 1

'if clicked word is not last word
If (Me.Text.IndexOf(" ", m_intClickIndex) <> -1) Then
m_intStartIndex = Me.Text.IndexOf(" ", m_intClickIndex)
End If

'moving towords starting of string from clicked position
Dim m_intLastIndex As Int16 = m_strInitialString.LastIndexOf(" ")

'original clicked word
Dim m_strWord As String = Me.Text.Substring(m_intLastIndex + 1, m_intStartIndex - m_intLastIndex)

If (m_strWord.Length > 0) Then
Dim m_doc As Word.Document = m_app.Documents.Add()

Dim m_listOfAlternateWords As Word.SpellingSuggestions = m_app.GetSpellingSuggestions(m_strWord)

If m_listOfAlternateWords.Count > 0 Then
m_objContextMenuStrip.Items.Clear()

Dim m_word As Integer
For m_word = 1 To m_listOfAlternateWords.Count
Dim Item As New ToolStripMenuItem()
Item.Name = m_listOfAlternateWords.Item(m_word).Name
Item.Text = Item.Name

Item.Tag = New Int16() {m_intLastIndex, m_intStartIndex}

AddHandler Item.Click, AddressOf ToolStripMenuItem_Click

m_objContextMenuStrip.Items.Add(Item)
Next

If (m_objContextMenuStrip.Items.Count > 0) Then
m_objContextMenuStrip.Show(Me, New Point(e.X, e.Y))
End If
End If

End If

m_objContextMenuStrip = Nothing

End If
' End If
End Sub

Private Sub SpellCheckTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
' UnderLineWrongWords()
'MessageBox.Show(sender.ToString())
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (m_app Is Nothing) Then
Dim m_saveChanges As Object = False
Dim m_originalFormat As Object = Missing.Value
Dim m_routeDocument As Object = Missing.Value
'm_app.Documents.Close(False)
m_app.Quit(m_saveChanges, m_originalFormat, m_routeDocument)
m_app = Nothing
End If
End If
MyBase.Dispose(disposing)
End Sub


#End Region

'#Region "Methods"


Private Sub InitiatVariable()
If (m_app Is Nothing) Then
Dim m_app As Word.Application = New Word.Application()
Dim m_doc As Word.Document = m_app.Documents.Add()

End If
End Sub
Private Function IsWrongWord(ByVal m_strWord As String) As Boolean
Dim m_listOfAlternateWords As Word.SpellingSuggestions = m_app.GetSpellingSuggestions(m_strWord)
If (m_listOfAlternateWords.Count > 0) Then
Return True
Else
Return False
End If
End Function

Private Sub UnderLineWrongWords()
Me.SelectionStart = 0
'Dim app As Word.Application = New Word.Application()
Dim m_range As Word.Range

'm_app.Documents.Add()
m_range = m_app.ActiveDocument.Range
m_range.InsertAfter(Me.Text)

Dim m_spellCollection As Word.ProofreadingErrors = m_range.SpellingErrors
Dim m_intWord As Integer
Dim m_font As Font = Me.SelectionFont
Dim m_strIndex As Int16 = 0

For m_intWord = 1 To m_spellCollection.Count
Me.Find(m_spellCollection.Item(m_intWord).Text, m_strIndex, RichTextBoxFinds.WholeWord) ', RichTextBoxFinds.WholeWord, RichTextBoxFinds.WholeWord)
m_strIndex = Me.Text.IndexOf(m_spellCollection.Item(m_intWord).Text, m_strIndex)
Me.SelectionFont = New Font(m_font.FontFamily, m_font.Size, FontStyle.Underline)
Next
Me.SelectionStart = Me.SelectionStart + Me.SelectionLength + 1

Me.Refresh()
End Sub

Private Sub SpellChecking()

If (Me.Text.Length > 0) Then

m_app.Visible = False

m_app.WindowState = 0

Dim m_template As Object = Missing.Value
Dim m_newTemplate As Object = Missing.Value
Dim m_documentType As Object = Missing.Value
Dim m_visible As Object = False
Dim m_optional As Object = Missing.Value

Dim m_doc As Word.Document = m_app.Documents.Add(m_template, m_newTemplate, m_documentType, m_visible)

m_doc.Words.First.InsertBefore(Me.Text)
Dim m_we As Word.ProofreadingErrors = m_doc.SpellingErrors

m_doc.CheckSpelling(m_optional, m_optional, m_optional, m_optional, m_optional, m_optional, m_optional, m_optional, m_optional, m_optional, m_optional, m_optional)


Dim m_first As Object = 0
Dim m_last As Object = m_doc.Characters.Count - 1

Me.Text = m_doc.Range(m_first, m_last).Text

Dim m_saveChanges As Object = False
Dim m_originalFormat As Object = Missing.Value
Dim m_routeDocument As Object = Missing.Value
m_app.Quit(m_saveChanges, m_originalFormat, m_routeDocument)

Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.SelectionFont = New Font(Me.SelectionFont.FontFamily, Me.SelectionFont.Size, FontStyle.Regular)
Me.Refresh()
Me.SelectionStart = Me.Text.Length
m_app = New Word.Application()
m_doc = m_app.Documents.Add()

End If
End Sub

Private Sub InitializeComponent()
Me.SpellCheckTextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'SpellCheckTextBox1
'
Me.SpellCheckTextBox1.Location = New System.Drawing.Point(0, 0)
Me.SpellCheckTextBox1.Name = "SpellCheckTextBox1"
Me.SpellCheckTextBox1.Size = New System.Drawing.Size(100, 20)
Me.SpellCheckTextBox1.TabIndex = 0
'
'SpellCheckTextBox
'
Me.ResumeLayout(False)

End Sub
End Class

Continue reading...
 
Back
Top