Richtextbox vb.net

underline word under mouse

Page 1 of 1

1 Replies - 8052 Views - Last Post: 20 August 2010 - 06:20 AM Rate Topic: -----

#1 siri1008   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-January 10

Richtextbox vb.net

Posted 20 August 2010 - 01:01 AM

Hi dear all,

In my program i have made a Module to get word under mouse in this way

'====================================================
Module Module1
Public Function GetWordUnderMouse(ByRef Rtf As System.Windows.Forms.RichTextBox, ByVal X As Integer, ByVal Y As Integer) As String
On Error Resume Next
Dim POINT As System.Drawing.Point = New System.Drawing.Point()
Dim Pos As Integer, i As Integer, lStart As Integer, lEnd As Integer
Dim lLen As Integer, sTxt As String, sChr As String
'
POINT.X = X
POINT.Y = Y
GetWordUnderMouse = vbNullString
'
With Rtf
lLen = Len(.Text)
sTxt = .Text
Pos = Rtf.GetCharIndexFromPosition(POINT)
If Pos > 0 Then
For i = Pos To 1 Step -1
sChr = Mid(sTxt, i, 1)
If sChr = " " Or sChr = Chr(13) Or i = 1 Then
'if the starting character is vbcrlf then
'we want to chop that off
If sChr = Chr(13) Then
lStart = (i + 2)
Else
lStart = i
End If
Exit For
End If
Next i
For i = Pos To lLen
If Mid(sTxt, i, 1) = " " Or Mid(sTxt, i, 1) = Chr(13) Or i = lLen Then
lEnd = i + 1
Exit For
End If
Next i
If lEnd >= lStart Then
GetWordUnderMouse = Trim(Mid(sTxt, lStart, lEnd - lStart))
End If
End If
End With
End Function
End Module
'====================================================
It works

also i need to underline the word which the mouse is on it.
if anybody know how to do this please reply me

here is the other part of the program (Form1)

'====================================================
Public Class Form1
Dim word As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
'Populate richtext box with some text
Me.RichTextBox1.Text = "This example shows you how to get the word that is currently under the mouse pointer of this window and displays it in the text box below. Hover your mouse pointer over the window and you will see what i mean :)! Happy .NET coding!"
End Sub

Private Sub RichTextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseClick
TextBox1.Text = word
End Sub

Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
On Error Resume Next
word = GetWordUnderMouse(Me.RichTextBox1, e.X, e.Y)

If Not word = "" Then
RichTextBox1.Cursor = Cursors.Hand
Else
RichTextBox1.Cursor = Cursors.Default
End If

End Sub

End Class
'====================================================

thanks all

regards Chandana

Is This A Good Question/Topic? 0
  • +

Replies To: Richtextbox vb.net

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Richtextbox vb.net

Posted 20 August 2010 - 06:20 AM

Please use code tags in your posts. You can do this by hilighting the code and clicking on the <> in the toolbar.

In your MouseClick event, add the following code.

RichTextBox1.Find(word)
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Underline)


You will however need to edit this code to find the exact word as the find will find the first occurrence and underline it regardless of where else you click the same word. I'll try to play around with it later if time permits to see if I can give a more accurate example for the exact word you are clicking. It should be a good start though.

UPDATE:
I'm sure there is an easier way to do this, afterall, you can select a word in a richtextbox by double-clicking it but using your code I came up with a way but it's not a elegant as I would like it since it uses global variables but here goes.

In your Module, create the following outside the Function
Public startPOS as integer
Public endPOS as integer

Next, change your code to this inside your function
If lEnd >= lStart Then
         GetWordUnderMouse = Trim(Mid(sTxt, lStart, lEnd - lStart))
         startPOS = lStart
         endPOS = lEnd
      End If


Since your function has determined the start and end of the word, we will store these positions and pass them to the find method in the richtextbox so that it will find the word you're clicking on and not the first occurrence of the word earlier in the paragraph.

Lastly, in your RichTextBox1.MouseClick event you would use:
  RichTextBox1.Find(word, startPOS, endPOS, RichTextBoxFinds.WholeWord)
  RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Underline)

This post has been edited by CharlieMay: 20 August 2010 - 07:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1