Hi guys,
could somebody here tell me how to color the current text line in richTextBox Object, or where can I search for this feature???
Thanks
[EL-Prince]
Hi guys,
could somebody here tell me how to color the current text line in richTextBox Object, or where can I search for this feature???
Thanks
[EL-Prince]
Short summary of the thread: wants the current text line highlighted without showing a selection. correctly pointed out that you can programmatically select the line and change SelectionBackColor (which is the simplest route) but that approach alters the text formatting in the RTF. Two practical options:
SelectionBackColor, restore selection). See SelectionBackColor for details: RichTextBox.SelectionBackColor.The subclass below shows a lightweight visual-only method (VB.NET). It listens for paint and scrolling and draws a semi-transparent rectangle at the caret line using GetPositionFromCharIndex:
Public Class LineHighlightRichTextBox
Inherits RichTextBox
Private Const WM_PAINT As Integer = &HF
Private Const WM_VSCROLL As Integer = &H115
Private Const WM_MOUSEWHEEL As Integer = &H20A
Private Const WM_HSCROLL As Integer = &H114
Private ReadOnly HighlightBrush As Brush = New SolidBrush(Color.FromArgb(40, 255, 255, 0))
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case WM_PAINT
DrawLineHighlight()
Case WM_VSCROLL, WM_MOUSEWHEEL, WM_HSCROLL
Invalidate()
End Select
End Sub
Private Sub DrawLineHighlight()
Using g = Graphics.FromHwnd(Handle)
Dim caretIndex = SelectionStart
Dim p = GetPositionFromCharIndex(caretIndex)
If p.Y >= 0 Then
Dim h = Font.Height
Dim r = New Rectangle(0, p.Y, ClientSize.Width, h)
g.FillRectangle(HighlightBrush, r)
End If
End Using
End Sub
Protected Overrides Sub OnKeyUp(e As KeysEventArgs)
MyBase.OnKeyUp(e)
Invalidate()
End Sub
Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
MyBase.OnMouseUp(e)
Invalidate()
End Sub
End Class Troubleshooting notes: wrapping vs. logical lines is a common gotcha — visual highlighting above follows the caret's visual line (use GetPositionFromCharIndex), whereas the selection-based approach uses the logical newline boundaries. Invalidate on key, mouse and scroll events to keep the highlight in sync; test for flicker and IME/input-method corner cases. For the persistent approach refer to SelectionBackColor linked above.
Jump to Post— privatevoid 24Look at
RichTextBox.Select(), see if the code below helps:// The draw back to this is if a line wraps it will continue the // selection until a newline char is found. int startIndex = myRtfEdit.GetFirstCharIndexOfCurrentLine(); int selectionLength = myRtfEdit.Text.IndexOf('\n', startIndex) - startIndex; if(selectionLength > …
Look at RichTextBox.Select() , see if the code below helps:
// The draw back to this is if a line wraps it will continue the
// selection until a newline char is found.
int startIndex = myRtfEdit.GetFirstCharIndexOfCurrentLine();
int selectionLength = myRtfEdit.Text.IndexOf('\n', startIndex) - startIndex;
if(selectionLength > 0) myRtfEdit.Select(startIndex, selectionLength); Hi,
Thanks about answering my question, but actually this NOT what I want.
What I want is highlight or coloring the current row of text in richTextBox object, not selecting text.
Thanks
[EL-Prince]
Actually it is key to what you want to do. To do most things in a RichTextBox you need to select the text you wish to alter so selecting that text can be the most complicated part.
with the above in mind consider the following:
myRtfEdit.SelectionColor = Color.Blue;
myRtfEdit.SelectionFont = myFont;
myRtfEdit.SelectionBackColor = Color.Gray;
myRtfEdit.DeselectAll(); We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.