Question Working with RTF in a RichTextBox

TomK

Member
Joined
Jul 31, 2008
Messages
5
Programming Experience
Beginner
first let me start by posting code..
VB.NET:
 Private Sub checkforlinks(ByVal text As String)
        Dim re As New Regex("{\w+}")
        Dim m As Match = re.Match(text)
        While m.Success = True
            rtb.InsertLink(m.ToString, m.Index)
            m = m.NextMatch
        End While
    End Sub

rtb is a rich text box that i'm declaring for my entire form.. It is actually an instance of a Richtextbox but derived from a class i found that allows adding of links. (hence the "InsertLink()" )

Any Here is my problem: I'm trying parse the text in the rtb for strings that look like {xxxxx}. When i find them, I want to removed the plain text {xxxxx} AND send it to my InsertLink() method. What run into is a problem when i have two items (eg. {xxxxx} and {yyyyyy}) in the textbox. I've been able to remove the plain text and insert a link with only one, but when i have two the rtf format gets messed up and the first link goes away.

it happens when i do rtb.text = String .. that takes away the rtf data thus taking away all my links excpet for my last one.


I'm looking for a line of code or lines.. that lets me .Remove("{xxxxxx}") from my rtb.rtf rather than from my rtb.text..

Thanks in advance for any help that is provided..

Please just call me an idiot if i'm being to unclear : p

Tom
 
m.NextMatch will not work when you change the text between calls, NextMatch does not make a new query and you will get the indexes wrong. While looping Success you have to remove + insert then make a new Regex query to find next match. Removing a string from rtb.Text also removes it from rtb.Rtf, you are probably just confused because you had the indexes messed up and the text/rtf mangled.
 
so you're saying something like:


VB.NET:
Private Sub checkforlinks(ByVal text As String)
        dim bolVar as boolean = true
        While bolVar = True
            Dim re As New Regex("{\w+}")
            Dim m As Match = re.Match(text)
            bolVar = m.success

            '' remove line here ? 

            rtb.InsertLink(m.ToString, m.Index)
          
        End While
End Sub

Using rtb.text.remove() does nothing if i don't do something like

VB.NET:
rtb.text = rtb.text.remove()

and if i do that i lose my RTF formatting... in otherwords all links generated before that line.
 
Last edited:
I'm thinking maybe i should just (make note of the index, remove text, take note of index, remove text, looped) then add links where the indexs are...
 

Latest posts

Back
Top