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!
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.
Re: rich text box highlight line method
Hi,
You can try this:
vb Code:
Private Sub button1_Click(sender As Object, e As EventArgs)
Me.richTextBox1.HideSelection = False
SelectLine(2, Me.richTextBox1)
End Sub
Private Sub SelectLine(lineNumber As Integer, richTextBox As RichTextBox)
Dim begin As Integer = 0
' add the length of the lines
For i As Integer = 0 To lineNumber - 1
begin += richTextBox.Lines(i).Length
' don't forget to add one more for the Environment.NewLine
begin += 1
Next
Dim length As Integer = richTextBox.Lines(lineNumber).Length
richTextBox.[Select](begin, length)
End Sub
Wkr,
sparrow1