TextEdit.CustomHighlightText Event
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.v24.2.dll
Declaration
[DXCategory("Events")]
public event TextEditCustomHighlightTextEventHandler CustomHighlightText
Event Data
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. |
Remarks
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.
- Highlight strings with custom foreground and background colors.
- Add padding for text blocks.
- Specify a painter that draws text using custom font settings.
- Specify a painter that replaces text blocks with custom graphics (for instance, icons or smileys).
Example
The following example handles the CustomHighlightText
event to highlight and custom paint text in a MemoEdit:
- Strikethrough a text block at the beginning of the text.
- Draw a star icon instead of the ‘star’ string.
- Change the background and foreground colors for individual words and strings.
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;
}
}