Search code examples
vb.netrichtextboxvb.net-2010rtf

How to Loop through BOLD lines over Richtextbox?


Richtextbox text is :

Tried code :

for I as integer=0 to richtextboxBold.lines.length-1

   If islineBold(richtextboxBold.liines(I)) then 

      richtextboxOutput.AppendText("Line "& I &" is Bold")
   Else

      richtextboxOutput.AppendText("Line "& I &" is Not Bold")

   End If

Next

Function islineBold(byval str as string) as Boolean

End Function

Output Required :

Line 1 is Bold
Line 2 is Not Bold


Solution

  • Try something like this:

    Dim sLine As String        
    Dim iCont As Integer
    
    For (i As Integer = 0 To rt.Lines.Lenght - 1)
        sLine = rt.Lines(i)
        rt.Select(iCont, sLine.Length - 1)
    
        If rt.SelectionFont.Bold Then
            richtextboxOutput.AppendText("Line "& i + 1 &" is Bold")
        Else
            richtextboxOutput.AppendText("Line "& i + 1 &" is Not Bold")
        End If
    
        iCont += sLine.Length
    Loop
    

    Where rt is the RichTextBox. This will check if the whole line is bold.

    You can't test if something is bold passing only a string to the function, as the only option then is to find the text into the RichTextBox and could be repeated. If you want to do a function you could do something like this:

    Function IsBoldText(rt As RichTextBox, start As Integer, length As Integer)
        rt.Select(start, length)
        Return rt.SelectionFont.Bold
    End Function