windowsforms-devexpress-dot-xtraeditors-dot-textedit-c8f17394.md
Allows you to highlight or custom paint strings within the control’s text. This event is supported in Advanced mode (see RepositoryItemTextEdit.UseAdvancedMode).
Namespace : DevExpress.XtraEditors
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
[DXCategory("Events")]
public event TextEditCustomHighlightTextEventHandler CustomHighlightText
<DXCategory("Events")>
Public Event CustomHighlightText As TextEditCustomHighlightTextEventHandler
The CustomHighlightText event's data class is TextEditCustomHighlightTextEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| LineIndex | Gets the zero-based index of the currently processed text line for multi-line text. Returns 0 for single-line text. |
| Text | Gets the currently processed text/text line (for multi-line text). |
| TextPosition | Gets the position of the currently processed text line within the editor’s text. |
The event data class exposes the following methods:
| Method | Description |
|---|---|
| HighlightRange(Int32, Int32, Action<TextEdit.Block>) | Highlights or custom paints a text block at a specified position. |
| HighlightRange(Int32, Int32, Color, Color) | Highlights a text block at a specific position using custom foreground and background colors. |
| HighlightRange(Int32, Int32, Color) | Highlights a text block at a specified position using a custom foreground color. |
| HighlightRanges(String, Action<TextEdit.Block>, CompareOptions) | Highlights or custom paints all text blocks that have the specified content. |
| HighlightRanges(String, Color, Color, CompareOptions) | Highlights all text blocks that have specified content using custom foreground and background colors. |
| HighlightRanges(String, Color, CompareOptions) | Highlights all text blocks that have the specified content using a custom foreground color. |
| HighlightWords(String, Action<TextEdit.Block>, CompareOptions) | Highlights or custom paints whole words. |
| HighlightWords(String, Color, Color, CompareOptions) | Highlights whole words using custom foreground and background colors. |
| HighlightWords(String, Color, CompareOptions) | Highlights whole words using a custom foreground color. |
You can handle the CustomHighlightText (or RepositoryItemTextEdit.CustomHighlightText) event to highlight or paint individual strings within the control’s text in a custom manner.
Use the event’s Text parameter to identify the control’s text. For multi-line text (for example, in a MemoEdit control), the CustomHighlightText event fires repeatedly for each text line, and the Text parameter specifies the currently processed text line.
The HighlightRange, HighlightRanges and HighlightWords methods (available from the event’s e argument) allow you to perform the following tasks.
The following example handles the CustomHighlightText event to highlight and custom paint text in a MemoEdit:
using DevExpress.XtraEditors;
// Enable Advanced Mode during the control's initialization (for instance, at design time)
memoEdit1.Properties.UseAdvancedMode = DevExpress.Utils.DefaultBoolean.True;
memoEdit1.Text = "The formation of a star begins with gravitational instability within a molecular cloud, caused by regions of higher density—often triggered by compression of clouds by radiation from massive stars \r\n (Wikipedia)";
memoEdit1.Font = new Font("Tahoma", 10);
private void memoEdit1_CustomHighlightText(object sender, TextEditCustomHighlightTextEventArgs e) {
// Change a word's background color and add padding.
e.HighlightWords("with", (block) => {
block.BackColor = Color.Yellow;
block.Padding = new Padding(5, 0, 5, 0);
});
// Change a string's background and foreground colors.
e.HighlightRanges("cloud", (block) => {
block.BackColor = Color.Yellow;
block.ForeColor = Color.Red;
});
// Strikethrough text at the beginning of the first line.
if (e.LineIndex == 0)
e.HighlightRange(0, 12, (block) => { block.Painter = new MyStrikethroughTextPainter(Color.Gray); });
// Draw a star icon instead of the 'star' string.
e.HighlightRanges(
"Star",
(block) => {
block.Painter = new MyImagePainter();
block.ContentSize = new Size(14, 14);
block.AllowNavigation = false;
},
System.Globalization.CompareOptions.IgnoreCase);
}
public class MyStrikethroughTextPainter : TextEdit.TextEditBlockPainter {
public MyStrikethroughTextPainter(Color color) {
Color = color;
}
Color Color { get; set; }
static readonly Font StrikeoutFont = new Font("Tahoma", 10f, FontStyle.Strikeout);
public override bool DrawForeground(TextEdit.Block block) {
DrawString(block.Text, StrikeoutFont, block.Segments[0].TextBounds, Color);
return true;
}
}
public class MyImagePainter : TextEdit.TextEditBlockPainter {
static Bitmap img;
static Bitmap Image {
get {
if (img == null) {
using (var temp = System.Drawing.Image.FromFile("d:\\star.png"))
img = new Bitmap(temp);
}
return img;
}
}
public override bool DrawForeground(TextEdit.Block block) {
DrawBitmap(Image, block.Segments[0].Bounds, new RectangleF(Point.Empty, Image.Size));
return true;
}
}
Imports DevExpress.XtraEditors
' Enable Advanced Mode during the control's initialization (for instance, at design time).
MemoEdit1.Properties.UseAdvancedMode = DevExpress.Utils.DefaultBoolean.True
MemoEdit1.Text = "The formation of a star begins with gravitational instability within a molecular cloud, caused by regions of higher density—often triggered by compression of clouds by radiation from massive stars " & Constants.vbCrLf & " (Wikipedia)"
MemoEdit1.Font = New Font("Tahoma", 10)
Private Sub MemoEdit1_CustomHighlightText(sender As Object, e As DevExpress.XtraEditors.TextEditCustomHighlightTextEventArgs) Handles MemoEdit1.CustomHighlightText
' Change a word's background color and add padding.
e.HighlightWords("with", Sub(block)
block.BackColor = Color.Yellow
block.Padding = New Padding(5, 0, 5, 0)
End Sub)
' Change a string's background and foreground colors.
e.HighlightRanges("cloud", Sub(block)
block.BackColor = Color.Yellow
block.ForeColor = Color.Red
End Sub)
' Strikethrough text at the beginning of the first line.
If e.LineIndex = 0 Then e.HighlightRange(0, 12, Sub(block)
block.Painter = New MyStrikethroughTextPainter(Color.Gray)
End Sub)
' Draw a star icon instead of the 'star' string.
e.HighlightRanges("Star", Sub(block)
block.Painter = New MyImagePainter()
block.ContentSize = New Size(14, 14)
block.AllowNavigation = False
End Sub, System.Globalization.CompareOptions.IgnoreCase)
End Sub
Public Class MyStrikethroughTextPainter
Inherits TextEdit.TextEditBlockPainter
Public Sub New(ByVal color As Color)
color = color
End Sub
Private Property Color As Color
Shared ReadOnly StrikeoutFont As Font = New Font("Tahoma", 10F, FontStyle.Strikeout)
Public Overrides Function DrawForeground(ByVal block As TextEdit.Block) As Boolean
DrawString(block.Text, StrikeoutFont, block.Segments(0).TextBounds, Color)
Return True
End Function
End Class
Public Class MyImagePainter
Inherits TextEdit.TextEditBlockPainter
Shared img As Bitmap
Private Shared ReadOnly Property Image As Bitmap
Get
If img Is Nothing Then
Using temp = System.Drawing.Image.FromFile("d:\star.png")
img = New Bitmap(temp)
End Using
End If
Return img
End Get
End Property
Public Overrides Function DrawForeground(ByVal block As TextEdit.Block) As Boolean
DrawBitmap(Image, block.Segments(0).Bounds, New RectangleF(Point.Empty, Image.Size))
Return True
End Function
End Class
See Also