Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to replace the current word under mouse cursor when user click on contextmenustrip dorpdownitem just like synonymous option in MS office word. i have word to replace and word for replace. but iam unable to get the exact location of word i want to replace. here is the code.

VB
Private Sub tsmmutradifat__DropDownItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tsmmutradifat_.DropDownItemClicked
    Dim myclickeditem As String = correct_word(e.ClickedItem.Text)
    Dim wordtoreplace As String
    If Not myclickeditem = Nothing Then
        If RichTextBox1.SelectedText = "" Then
            ' Code required
        Else
            wordtoreplace = RichTextBox1.SelectedText
            If wordtoreplace.EndsWith(" ") Then
                myclickeditem = myclickeditem & " "
            End If
            If wordtoreplace.StartsWith(" ") Then
                myclickeditem = " " & myclickeditem
            End If
            RichTextBox1.SelectedText = myclickeditem
        End If
    End If
End Sub
Posted
Updated 10-May-12 21:15pm
v3
Comments
E.F. Nijboer 11-May-12 5:24am    
You mean selection/caret position instead of mouse position ;-) Would otherwise impossible because the mouse is at the drop down box.
hackerspk 11-May-12 5:26am    
yes you are right.iam unable to find exact word on it

1 solution

ok made find the below code on a forum and changed it according to my need it a little odd but working.
In mouse move envent
VB
    Public cursorAt As Integer
    Public wordEnd As Integer
    Public isEnd As Boolean
    Public wordStart As Integer
cursorAt = RichTextBox1.GetCharIndexFromPosition(e.Location)
       If cursorAt = 0 Then Return
       wordEnd = New Regex("[\s$]{1}").Match(RichTextBox1.Text.Substring(cursorAt)).Index
       isEnd = Not New Regex("\s").IsMatch(RichTextBox1.Text.Substring(cursorAt))
       wordStart = New Regex("[\s^]{1}", RegexOptions.RightToLeft).Match(RichTextBox1.Text.Substring(0, cursorAt)).Index
       If isEnd Then
 RichTextBox1.Select(wordStart, RichTextBox1.Text.Length)
                  Else
          RichTextBox1.Select(wordStart, cursorAt - wordStart + wordEnd)
          
       End If


and then simply replace then word

VB
RichTextBox1.SelectedText = myclickeditem
 
Share this answer
 
Comments
Maciej Los 11-May-12 11:26am    
Bravo! +5

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900