Resolved problem in selecting the underlined text in richtextbox

jamesvick

Active member
Joined
Jul 19, 2010
Messages
34
Programming Experience
1-3
Hello, i have a simple notepad application where i want a feature of selecting text which is underlined. Usually what happens is when i click a button the app reads a text file and underlines random text in the richtextbox. What i want is on the click of another button to select the text which is underlined. I thought of creating char range, regex all but not getting anywhere. I also dunno how to build a regex.

What i know is since all RTF texts are different we can choose the rtf text instead of simple text to select the underlined text. The rtf of underlined text in my application is this :

VB.NET:
\ul\i0 so many\ulnone\i

where ul and ulnone mark the text as underlined. Now i want to select all such texts which are surrounded by \ul\i0 and \ulnone\i . I thought of the following method :

1) Create another richtextbox2
2) Paste the text of richtextbox1 as rtf in richtextbox2. I mean show the text in coded rtf (like above) instead of normal rtf.
3) Use simple find to select all the text which is surrounded by ul and ulnone tags
4) process the text.

But this step is very tedious. If anyone could help me in making a simple procedure or build a regex which could process the text surrounded by those tags, i would be very thankful.
 
Last edited:
VB.NET:
Imports System.Text.RegularExpressions

Public Class Form1
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim i As String = IO.File.ReadAllText("C:\users\home\desktop\test.txt")
          'remove all spaces and returns
          i = Regex.Replace(i, "\s{2,}", "")

          'loop through i to find all the tags
          Do
               Try
                    'get the start and stop points
                    Dim iStart As Integer = InStr(i, "\ul\") + 4
                    Dim iEnd As Integer = InStr(i, "\ulnone\") - 5
                    
                    'add text between the start and stop points
                    ListBox1.Items.Add(Microsoft.VisualBasic.Mid(i, iStart, iEnd))
                    
                    'remove all text from the variable up to the stop point
                    i = Microsoft.VisualBasic.Right(i, Len(i) - (iEnd + 13))
               Catch
                    Exit Do
               End Try
          Loop
     End Sub
End Class

hope this works for ya... did for me. :)
 
hi thanks for reply.my first sentence is this :

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\f0\fs20 Why\i \ul\i0 so many\ulnone\i \ul\i0 software\ulnone\i \i0 fail\i ?\i0\par

and what i get in listbox is this :

i0 so many\ulnone\i \ul\i0 software\ulnone\i \i0 fail\i ?\i0\par


so iam confused how this works?

my code :

VB.NET:
RichTextBox5.Text = rtfArticle.Rtf

        Dim str As String

        str = Regex.Replace(RichTextBox5.Text, "\s{2,}", "")

        'loop through i to find all the tags
        Do
            Try
                'get the start and stop points
                Dim iStart As Integer = InStr(str, "\ul\") + 4
                Dim iEnd As Integer = InStr(str, "\ulnone\") - 5

                'add text between the start and stop points
                ListBox1.Items.Add(Microsoft.VisualBasic.Mid(str, iStart, iEnd))

                'remove all text from the variable up to the stop point
                str = Microsoft.VisualBasic.Right(str, Len(str) - (iEnd + 13))

                'MsgBox(str)
            Catch
                Exit Do
            End Try
        Loop

and why is it -5?
 
VB.NET:
Dim i As String = IO.File.ReadAllText("C:\users\home\desktop\test.txt")
        i = Regex.Replace(i, "\s{2,}", "")
        Dim x() As String = Regex.Split(i, "(?:\\ulnone\\).*?(?:\\ul\\)")
        For Each item As String In x
            If item.StartsWith("\ul\") Then item = Microsoft.VisualBasic.Right(item, Len(item) - 4)
            ListBox1.Items.Add(item)
        Next

try this one... i just had to figure out the pattern.

edit...

oh that wont work either because of the kinds of characters you have in the string
 
Last edited:
VB.NET:
 Dim i As String = IO.File.ReadAllText("C:\users\home\desktop\test.txt")
 i = Regex.Replace(i, "\s{2,}", "")
 Do
      Try
            'get the start and stop points
            Dim iStart As Integer = InStr(i, "\ul\")
            Dim iEnd As Integer = InStr(i, "\ulnone\") - 4
            'add text between the start and stop points
            ListBox1.Items.Add(Microsoft.VisualBasic.Mid(i, iStart + 4, (iEnd - iStart)))
            'remove all text from the variable up to the stop point
            i = Microsoft.VisualBasic.Right(i, Len(i) - (iEnd + 8))
      Catch
            Exit Do
      End Try
 Loop

This seems to work with the data you are working with. hopefully. again, works for me.
 
hey very close but now i get this :

i0 so many
i0 software

i thought of using :

ListBox1.Items.Add(Microsoft.VisualBasic.Mid(i, iStart + 4, (iEnd - iStart)).Split("i0 ")(1))

to split and accept the later part of the string returned but it still leaves with

0 so many
0 software

there is something wrong about +-3.

when i do :

ListBox1.Items.Add(Microsoft.VisualBasic.Mid(i, iStart + 7, (iEnd - iStart)))

i get :

so many \ul
software \ul

So iam working to figure it out by testing. Anyways thanks for the help so far.
 
figured out the problem :

VB.NET:
Dim iEnd As Integer = InStr(i, "\ulnone\") - 7
                'add text between the start and stop points
                ListBox1.Items.Add(Microsoft.VisualBasic.Mid(i, iStart + 7, (iEnd - iStart)))


also rep added to you for help . :)
 
Im sorry i must have read wrong. thought you wanted all text between the \ul\ and \ulnone\ tags which i assumed included the i0. anyway, glad you got it worked out and thanks for the rep!
 
Back
Top