Click here to Skip to main content
15,914,608 members
Articles / Desktop Programming / Windows Forms

Spell check and underline the wrong word using Microsoft Office Word

Rate me:
Please Sign up or sign in to vote.
3.03/5 (20 votes)
7 Dec 2011CPOL4 min read 126.5K   2.9K   36   25
How to underline the wrong word using Microsoft Office Word
Screenshot - 1.jpg

Introduction

It's very hard when we are coding for some project and we require the functionality that we can think of for each part, but if we try to think of them together, we just can't logically arrange the parts.

I got the main idea to write this article when I was coding for one project and we were in need of a textbox which can check the spelling and it can underline the wrong word. But, the main requirement was that it should be achieved using Microsoft's Word Interop.

This article will show you how to use Word's spell check functionality and underline the wrong word in the textbox.

When the user starts to type in the textbox and presses the spacebar, our functionality will check the last typed word against Word's spell check functionality to verify the word. If it is wrong, that word will be underlined. And when the user right clicks on the underlined wrong word, user will see the menu with available options for that wrong word. Once the user will select the option from the menu, it will be replaced with the right word.

Code Exploring

To implement spell check functionality, you can write code directly on Form or you can create user control. If you want to create a user control to incorporate this functionality, you must user Richtextbox as the base class, since we are going to implement spell check functionality in Richtextbox.

Step 1: Add a new Class file under solution

We are going to create a control which will incorporate the functionality. So, add class (.vb) file under the solution. Add as I mentioned above, use RichTextBox as the base class.

VB.NET
Public Class SpellCheckTextBox
Inherits RichTextBox 

Step 2: Add reference

Now our class is ready, but we will need the reference of Microsoft Word's DLL. So, we will have to add a reference of Microsoft Word Object library. To add the reference, right click on Solution -> Add reference -> COM tab and select Microsoft Word object library.

Screenshot - 1.jpg

Now that a reference has been added to the project, we can use namespace for Office.

VB.NET
Imports Microsoft.Office 

Step 3: Start writing Events

First event that you need to add is Textbox's KeyUp event. This is because, when the user will start typing, we will identify the space and when the user presses spacebar it means that he/she has finished the word and we will need to verify it.

Write this code under KeyUp event of Textbox.

VB.NET
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 

Once the word is verified against Word's spell check functionality, if it is wrong then underline the word as a wrong word. In Richtextbox, if we want to change the format for a specific area, we need to change the selected text only.

Now, our wrong word(s) are underlined in Richtextbox. And our next step will be to replace it with a selection from the menu by right clicking on that wrong word.

We will achieve this by MouseDown event of RichTextbox. When we right click on a wrong word, our job is to pick that right clicked word. To select the right clicked word, we are using the GetCharIndexFromPosition function to get the index of right clicked character in the RichTextBox. Once, we get the index of that character, we will find the space from this index to the right and left.To do that, we will split the content of RichTextBox in three parts. Here is the code which will explain it all. Just paste this code in MouseDown event of RichTextBox.

VB.NET
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 length 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 towards 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

So, now we have the right clicked word. Send it to get the options from Word's spell check functionality. If there are multiple results, it means that word has suggestions. So, create a menu and use all the suggestions as menu items. We, are using Tag property of each menu item to store the starting and ending index of right clicked word. So, these two indexes will be used to replace the right clicked word with the selected option from the menu.

Here are some functions which I have used while coding.

VB.NET
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

When we select any option from the menu, we will replace the wrong word with the selected option by using Tag property of the menu item. Here is the code. Paste this in menu item click event.

VB.NET
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() 

It is also possible that somebody pastes the text in RichTextBox, so here, our underline functionality will be not used. So, we have to call the same functionality in the Leave event of RichTextBox. So, write this code in Leave event of RichTextBox.

VB.NET
UnderLineWrongWords()
Call SpellChecking() 

Mark that we are calling the spellcheck dialogue of Word when we underline the wrong word. We have already covered this function above.

At the end override the Dispose method.

VB.NET
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

So, here is the way to incorporate Word's spell check in our application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiona better approach Pin
yatzin20-Jan-13 9:44
yatzin20-Jan-13 9:44 
QuestionWord documents when correcting words Pin
panterall15-Aug-12 4:31
panterall15-Aug-12 4:31 
AnswerRe: Error Pin
qwertyuiop8825-Jan-12 10:14
qwertyuiop8825-Jan-12 10:14 
GeneralMy vote of 5 Pin
fymaxmada17-Jan-12 2:59
fymaxmada17-Jan-12 2:59 
GeneralVery nice written Pin
sudhansu_k1237-Dec-11 19:29
sudhansu_k1237-Dec-11 19:29 
GeneralGreat!!! Pin
Dirk Meusel16-Apr-10 7:25
Dirk Meusel16-Apr-10 7:25 
GeneralFull Source with VB project Pin
kazim bhai1-Jun-09 22:45
kazim bhai1-Jun-09 22:45 
GeneralRe:c# version with some little additions Pin
kazim bhai2-Jun-09 6:10
kazim bhai2-Jun-09 6:10 
GeneralGood except when it comes to new lines Pin
chriswininger14-Jan-09 5:02
chriswininger14-Jan-09 5:02 
GeneralRe: Good except when it comes to new lines Pin
yatzin17-Jan-13 16:53
yatzin17-Jan-13 16:53 
Questioncan someone provide sample project for this Pin
a kachanoff18-Sep-08 13:37
a kachanoff18-Sep-08 13:37 
QuestionWhat happens if user does not have Word installed ? Pin
UltraWhack16-Jun-08 4:39
UltraWhack16-Jun-08 4:39 
AnswerRe: What happens if user does not have Word installed ? Pin
chriswininger14-Jan-09 4:52
chriswininger14-Jan-09 4:52 
GeneralRe: What happens if user does not have Word installed ? Pin
yatzin17-Jan-13 16:51
yatzin17-Jan-13 16:51 
GeneralThanks Sir Pin
AbhiDilliwal6-Nov-07 3:02
AbhiDilliwal6-Nov-07 3:02 
QuestionErrors Pin
brownbag13-Sep-07 6:12
brownbag13-Sep-07 6:12 
QuestionNeed help with build errors Pin
j-dice17-Jul-07 7:16
j-dice17-Jul-07 7:16 
AnswerRe: Need help with build errors Pin
Aleksei Karimov28-Nov-07 6:20
Aleksei Karimov28-Nov-07 6:20 
AnswerRe: Need help with build errors Pin
chriswininger14-Jan-09 5:01
chriswininger14-Jan-09 5:01 
The ContextMenutStrip didn't exist in 2003. I had the same problem. You can use ContextMenu and MenuItem in place of ContextMenu and ToolStripMenuItem. The only problem is that MenuItem and does not have a Tag or Name property. In order to get around this I created my own classes and inherited from ContextMenu and MenuItem. I added each of the missing properties to MenuItem and I created a second Collection object and a sub called add. The add buts the Enhanced MenuItem into both my collection and the existing MenuItemCollection. I moved the code where he was adding a handler to each item into this Add Method and gave it the Address of Item_click. I then had item click raise an event which could be caught in SpellCheckTextBox. This even passes a reference to the Enhanced MenuItem. From there you can just take the code that was in his item click event and put it into this click event (minus his cast operation). Here's what my code looks like.

Public Class SpellCheckBox
    Inherits RichTextBox

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


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

    Private Sub SpellCheckBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If (Me.Text.Length = 0) Then
            Me.SelectionFont = New Font(Me.SelectionFont.FontFamily, _
                            Me.SelectionFont.Size, FontStyle.Regular)
            Me.SelectionColor = Me.ForeColor
            Me.Refresh()

        End If
        'MessageBox.Show(e.KeyValue)
        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.SelectionColor = System.Drawing.Color.Red
                Me.SelectionStart = Me.SelectionStart + Me.SelectionLength + 1
                Me.Refresh()
            End If
        ElseIf (e.KeyValue = 13) Then
            Dim m_strRecentWord As String = String.Empty
            Dim m_intLastIndex As Integer = Me.Text.Substring(0, Me.SelectionStart - 1).LastIndexOf(vbLf)

            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.SelectionColor = System.Drawing.Color.Red
                Me.SelectionStart = Me.SelectionStart + Me.SelectionLength + 1
                Me.Refresh()
            End If
        End If
    End Sub


    Private Sub ToolStripMenuItem_Click(ByVal m_item As MenuItemEnhance)
        'MessageBox.Show("blah")
        'Dim m_item As MenuItem = CType(sender, MenuItem)

        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.SelectionColor = Me.ForeColor
        Me.SelectedText = m_item.Text + " "

        Me.Refresh()


    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)
            Me.SelectionColor = System.Drawing.Color.Red
        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.SelectionColor = Me.ForeColor
            Me.Refresh()
            Me.SelectionStart = Me.Text.Length
            m_app = New Word.Application
            m_doc = m_app.Documents.Add()

        End If
    End Sub


    Private Sub SpellCheckBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        UnderLineWrongWords()
        Call SpellChecking()
    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

    Private Sub SpellCheckBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If (Me.SpellCheck = True) Then
            If (e.Button = Windows.Forms.MouseButtons.Right) Then
                Dim m_objContextMenuStrip As New ContextMenuEnhance
                AddHandler m_objContextMenuStrip.EnahancedClick, AddressOf ToolStripMenuItem_Click
                '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 length 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 towards 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.Clear()
                        Dim m_word As Integer
                        For m_word = 1 To m_listOfAlternateWords.Count
                            Dim Item As New MenuItemEnhance

                            Item.Name = m_listOfAlternateWords.Item(m_word).Name
                            Item.Text = m_listOfAlternateWords.Item(m_word).Name
                            Item.Tag = New Int16() {m_intLastIndex, m_intStartIndex}
                            'AddHandler Item.Click, AddressOf ToolStripMenuItem_Click
                            m_objContextMenuStrip.Add(Item)
                        Next
                        If (m_objContextMenuStrip.MenuItems.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
End Class

Public Class MenuItemEnhance
    Inherits MenuItem

    Private _tag As Object
    Private _name As String
    Private _items As Collection

    Public Property Tag() As Object
        Get
            Return Me._tag
        End Get
        Set(ByVal Value As Object)
            Me._tag = Value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return Me._name
        End Get
        Set(ByVal Value As String)
            Me._name = Value
        End Set
    End Property

End Class

Public Class ContextMenuEnhance
    Inherits System.Windows.Forms.ContextMenu

    Private items As New Collection

    Public Event EnahancedClick(ByVal item As MenuItemEnhance)

    Public Sub Add(ByVal item As MenuItemEnhance)
        Me.MenuItems.Add(item)
        Me.items.Add(item)
        AddHandler item.Click, AddressOf item_click
    End Sub

    Public Sub Clear()
        Me.MenuItems.Clear()
        If Me.items.Count > 0 Then
            Dim obj As Object
            Dim i As Integer = 0
            For Each obj In Me.items
                Me.items.Remove(i)
            Next
        End If
    End Sub

    Public Sub test()

    End Sub


    Private Sub item_click(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent EnahancedClick(items(CType(sender, MenuItem).Index + 1))
    End Sub
End Class



I hope this helps.
GeneralVery good. Pin
Anton Kratenok18-Jun-07 1:54
Anton Kratenok18-Jun-07 1:54 
GeneralRe: Very good. Pin
Mohib Sheth18-Oct-07 20:03
Mohib Sheth18-Oct-07 20:03 
GeneralExcellent job buddy ! Pin
infotechvish23-May-07 20:11
infotechvish23-May-07 20:11 
GeneralNice Pin
thund3rstruck18-May-07 4:24
thund3rstruck18-May-07 4:24 
GeneralFormatting... Pin
Jonathan [Darka]15-May-07 4:27
professionalJonathan [Darka]15-May-07 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.