Results 1 to 4 of 4

Thread: [RESOLVED] How to find index of line in RichTextBox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Resolved [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.

  2. #2
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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:
    1. Private Sub Button1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim offset As Int32 = 0
    3.     Dim Match As String = "The"
    4.  
    5.     ' Iterate through each line of text in the RichTextBox control.
    6.     For Each str As String In RichTextBox1.Lines
    7.         ' Look out for a match - Begins with the matching text (ignoring case).
    8.         If str.StartsWith(Match, StringComparison.CurrentCultureIgnoreCase) Then
    9.             RichTextBox1.SelectionStart = offset
    10.             RichTextBox1.SelectionLength = Match.Length
    11.             RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
    12.         End If
    13.  
    14.         ' Append the lines length to the offset.
    15.         offset += (str.Length+1) ' +1 for the new line characters (apparently not accounted for in the Length property).
    16.     Next
    17. 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.
    Last edited by DavesChillaxin; Feb 15th, 2013 at 04:46 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to find index of line in RichTextBox?

    Don't really need to use an offset marker ...

    vb.net Code:
    1. Dim names() As String = {"John", "Judy", "Jim"}
    2.         For Each n In names
    3.             For i = 0 To RichTextBox1.Lines.Length - 1
    4.                 If RichTextBox1.Lines(i).StartsWith(n) Then
    5.                     RichTextBox1.Select(RichTextBox1.GetFirstCharIndexFromLine(i), n.Length)
    6.                     RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
    7.                 End If
    8.             Next
    9.         Next
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: How to find index of line in RichTextBox?

    Thanks, that's what I needed. Appreciate the assist!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width