Answered selecting text in richtextbox which is underlined

jamesvick

Active member
Joined
Jul 19, 2010
Messages
34
Programming Experience
1-3
So how do we do it?

Text is like - hello vb dotnet forums !! you guys are the best coderz

On a button click i want to first select "vb dotnet forums" and on next click immediately select "are the best" (in a richtextbox control) . Iam not getting any idea as to how to proceed. Any help would really be appreciated.

P.S. I thought using regex but what would be the expression? Also is there any other method you guys know of or can point out?
 
Last edited:
These are characteristics of the font applied, not the text content, so Regex not an option (*). You would have to loop all char indexes and check if the font used includes the underline style.

(*) actually, it would be possible to find underlines by processing the Rtf where underline is denoted \ul both start and end, but mapping the rtf back to the visible text indexes is more than difficult.

Courtesy of now here is a code sample:
VB.NET:
Private range As CharacterRange

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    range = GetCharRange(FontStyle.Underline, range.First + range.Length)
    Me.RichTextBox1.Select(range.First, range.Length)
End Sub

Private Function GetCharRange(style As FontStyle, start As Integer) As CharacterRange
    Dim rangestart As Integer = -1
    For i As Integer = start To Me.RichTextBox1.TextLength - 1
        Me.RichTextBox1.Select(i, 1)
        Dim charstyle = Me.RichTextBox1.SelectionFont.Style
        If rangestart = -1 Then
            If charstyle.HasFlag(style) Then
                rangestart = i
            End If
        ElseIf Not charstyle.HasFlag(style) Then
            Return New CharacterRange(rangestart, i - rangestart)
        End If
    Next
End Function
 
hello john very much thanks for your code but i have one problem. Hasflag is an enum for vs2010 i suppose and is not in vs 2008. So can you please tell me an alternative for .net 3.5?

Also yup regex is not helping. The rtf uses \ul\ and \ulnone\ for underlining but i learned that different systems use a slightly tweaked rtf code.

EDIT : nevermined got charstyle.equals(style) and it works. amazing short codes. The forum would be so happy to have you here.
 
Last edited:
Hasflag is an enum for vs2010 i suppose
Yes, forgot that, it was added to Enum class in .Net 4. If you're only targeting the underline style and a char can only have one style then you can do 'If charstyle = style', but FontStyle is a flags enum where multiple values can be combined, the text may have a combination of styles and you can also search for a combination of styles, the equivalent 2008 code to HasStyle is this bitwise operation:
VB.NET:
If (charstyle And style) = style Then ' charstyle contains all flags in style
It was \ulnone for end in rtf code, true.
 
hi john iam stuck in another problem. Can you please guide me as to how to modify the above code to select words but when underline is detected select the whole phrase?

Eg : i love this forum very much

When i press F1 first I is selected, then love, then this then "forum very" and then much on each keypress.

Iam guessing that we can use if condition to check for a range of current position + 1. If the style is underlined then select phrase , if not then select the word. But how do i get the current position of editing in a richtextbox ? Also what style would a normal word have to select the word just like above? The only thing i can think about is spaces separating the words.
 
how do i get the current position of editing in a richtextbox ?
SelectionStart property is where cursor is at.
The only thing i can think about is spaces separating the words.
Any punctuation is also word delimiters, unless you want to include them as part of word (unusual).
 
selecting words has been solved - thnx to google and you only.

the code has been modified to split at punctuations :

VB.NET:
RichTextBox1.HideSelection = False
        Dim text As String = RichTextBox1.Text.Replace(vbLf, " ")

        Dim str As String = " " & "," & "(" & ")" & "." & "<" & ">" & "?" & "." & "/" & "\" & "*" & """" & ";" & ":" & "{" & "}" & "|"
        Dim words() As String = text.Split(str.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        If word >= words.Length Then
            word = 0
            index = 0
        End If
        index = RichTextBox1.Find(words(word), index, RichTextBoxFinds.None)
        RichTextBox1.Select(index, words(word).Length)
        index += words(word).Length
        word += 1


now this code moves next-next.

1) how can i start selecting from current position (selectionstart) rather than first position?

i know we have to assign word the current word number. But how do i get the current word number from current position?

2) What if want to move previous-previous (means moving backward) starting from current position? I tried this :

VB.NET:
RichTextBox1.HideSelection = False
        Dim text As String = RichTextBox1.Text.Replace(vbLf, " ")

        Dim str As String = " " & "," & "(" & ")" & "." & "<" & ">" & "?" & "." & "/" & "\" & "*" & """" & ";" & ":" & "{" & "}" & "|"
        Dim words() As String = text.Split(str.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        If word >= 0 Then
            word = words.Length - 1
            index = RichTextBox1.TextLength - 1
        End If
        index = RichTextBox1.Find(words(word), index, RichTextBoxFinds.Reverse)
        RichTextBox1.Select(index, words(word).Length)
        index -= words(word).Length
        word -= 1

but iam getting an error on 2nd click. First click selects the last word but on second click the find method assigns index a value of -1 which iam not getting. Any help plz?
 

Latest posts

Back
Top