[RESOLVED] How to find index of line in RichTextBox?
Okay, I've got a RichTextBox that the user enters data into. In this case, multiple lines from a script. I want to auto-detect if a line in the rtb is a line of dialogue in the play, i.e. does it begin with a character's name (I keep the names in a separate list). Detecting that is simple... I loop through the rtb.Lines and compare the beginning of each line to each name in the character array. The problem begins when I find a match... Let's say I want to make the first ten characters of line #7 bold... how? The way I know how to select characters and change the font of that selection depends upon a selection range that deals with absolute character position, not relative to a line number. So, is there an easy way to determine the index of a line, ex: lnie #7 begins at position 87 and ends at position 94? Or better yet, to say select text beginning at line #7, character 0, for 10 characters?
Honestly, it seems like it should be easy to do this... else why supply the .Lines in the first place? But I honestly cannot find it.
For the record, I'm using VS2010 and .NET 4.
Re: How to find index of line in RichTextBox?
Well I guess I understand your question.
To reiterate, you want to loop through each line in a RichTextBox control and bold the beginning text for whichever text it is your looking for? If so then give this a shot below...
vbnet Code:
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim offset As Int32 = 0
Dim Match As String = "The"
' Iterate through each line of text in the RichTextBox control.
For Each str As String In RichTextBox1.Lines
' Look out for a match - Begins with the matching text (ignoring case).
If str.StartsWith(Match, StringComparison.CurrentCultureIgnoreCase) Then
RichTextBox1.SelectionStart = offset
RichTextBox1.SelectionLength = Match.Length
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
End If
' Append the lines length to the offset.
offset += (str.Length+1) ' +1 for the new line characters (apparently not accounted for in the Length property).
Next
End Sub
The idea here is to keep track of the lines previous to the current one you're iterating through. This gives you an offset you can use when assigning your selection properties (these have no correlation to the Lines property). My understanding of the Lines property is just a readonly property that returns the text in an array split by the new line character.
Re: How to find index of line in RichTextBox?
Don't really need to use an offset marker ...
vb.net Code:
Dim names() As String = {"John", "Judy", "Jim"}
For Each n In names
For i = 0 To RichTextBox1.Lines.Length - 1
If RichTextBox1.Lines(i).StartsWith(n) Then
RichTextBox1.Select(RichTextBox1.GetFirstCharIndexFromLine(i), n.Length)
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
End If
Next
Next
Re: How to find index of line in RichTextBox?
Thanks, that's what I needed. Appreciate the assist!