Results 1 to 3 of 3

Thread: rich text box highlight line method

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    99

    rich text box highlight line method

    Hello,

    What is the method to highlight a line in a rich text box? I already know how to obtain the line numbers, pass the line number, etc.. I just need to know what the proper method is to highlight a line.

    For example, If i have 3 buttons labeled highlight line 1, line 2, line 3.

    How would I highlight the line?

    THANKS!

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: rich text box highlight line method

    Depending on what exactly you mean, I don't think it's as easy as you believe it to be.

    By 'highlighting', do you mean selecting the text, or changing the text or background color?

    If you mean selecting the text than that is not very hard. If you look close enough you can find functions and methods already defined in the RTB class that allow you to find the starting position (character number) and end position of one or multiple lines. You can then use the SelectionStart and SelectionLength properties to set the selection to that line.

    If you mean changing the background color (or text color) then you would do something similar, but after you selected the line you would set the SelectedBackcolor property (I believe that exists, if not, I'm sure there is a similar property). One problem here is though that it won't get 'de-highlighted' after you deselect it: you will need to clear the highlight (set the background back to normal) manually when you want it to revert.

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: rich text box highlight line method

    Hi,

    You can try this:



    vb Code:
    1. Private Sub button1_Click(sender As Object, e As EventArgs)
    2.          Me.richTextBox1.HideSelection = False
    3.          SelectLine(2, Me.richTextBox1)
    4.      End Sub
    5.    
    6.      Private Sub SelectLine(lineNumber As Integer, richTextBox As RichTextBox)
    7.          Dim begin As Integer = 0
    8.        
    9.          ' add the length of the lines
    10.          For i As Integer = 0 To lineNumber - 1
    11.              begin += richTextBox.Lines(i).Length
    12.              ' don't forget to add one more for the Environment.NewLine
    13.              begin += 1
    14.          Next
    15.        
    16.          Dim length As Integer = richTextBox.Lines(lineNumber).Length
    17.          richTextBox.[Select](begin, length)
    18.      End Sub

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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