Category / Section
How to highlight the given search text in consideration with text alignment in WinForms GridGroupingControl?
6 mins read
Highlight text
In order to highlight the search text in WinForms GridGroupingControl, TableControlDrawCellFrameAppearance event can be used. In this event, the search text rectangle will be found and highlighted based on the alignment.
C#
//Triggering the event
this.gridGroupingControl1.TableControlDrawCellFrameAppearance += gridGroupingControl1_TableControlDrawCellFrameAppearance;
//Event Customization
void gridGroupingControl1_TableControlDrawCellFrameAppearance(object sender, GridTableControlDrawCellBackgroundEventArgs e)
{
Color HighlightColor = Color.Yellow;
string text = HighlightText;
Graphics g = this.CreateGraphics();
//Find the search text rectangle for Left alignment text.
if (this.gridGroupingControl1.Appearance.AnyRecordFieldCell.HorizontalAlignment == GridHorizontalAlignment.Left)
{
HighlightLeftAlignedText(g, HighlightText, HighlightColor, e);
}
//Find the search text rectangle for Center alignment text.
else if (this.gridGroupingControl1.Appearance.AnyRecordFieldCell.HorizontalAlignment == GridHorizontalAlignment.Center)
{
HighlightCenterAlignedText (g, HighlightText, HighlightColor, e);
}
//Find the search text rectangle for Right alignment text.
else
{
HighlightRightAlignedText (g, HighlightText, HighlightColor, e);
}
}
VB
'Triggering the event
AddHandler Me.gridGroupingControl1.TableControlDrawCellFrameAppearance, AddressOf gridGroupingControl1_TableControlDrawCellFrameAppearance
'Event Customization
Private Sub gridGroupingControl1_TableControlDrawCellFrameAppearance(ByVal sender As Object, ByVal e As GridTableControlDrawCellBackgroundEventArgs)
Dim HighlightColor As Color = Color.Yellow
Dim text As String = HighlightText
Dim g As Graphics = Me.CreateGraphics()
'Find the search text rectangle for Left alignment text.
If Me.gridGroupingControl1.Appearance.AnyRecordFieldCell.HorizontalAlignment = GridHorizontalAlignment.Left Then
HighlightLeftAlignedText(g, HighlightText, HighlightColor, e)
'Find the search text rectangle for Center alignment text.
ElseIf Me.gridGroupingControl1.Appearance.AnyRecordFieldCell.HorizontalAlignment = GridHorizontalAlignment.Center Then
HighlightCenterAlignedText(g, HighlightText, HighlightColor, e)
'Find the search text rectangle for Right alignment text.
Else
HighlightRightAlignedText(g, HighlightText, HighlightColor, e)
End If
End Sub
Highlight
the Left Aligned Text
C#
private void HighlightLeftAlignedText(Graphics g, string highlightText, Color highlightColor,GridTableControlDrawCellBackgroundEventArgs e)
{
int value;
double doubleValue;
GridTableCellStyleInfo style = e.Inner.Style as GridTableCellStyleInfo;
if (!int.TryParse(e.Inner.Style.Text.ToString(), out value) && !double.TryParse(e.Inner.Style.Text, out doubleValue))
{
if (!string.IsNullOrEmpty(highlightText) && e.Inner.Style.Text.Contains(highlightText) && !(e.Inner.Style.WrapText || e.Inner.Style.AllowEnter))
{
var s = WinFormsUtils.MeasureSampleWString(g, e.Inner.Style.GdipFont);
Size textSize = g.MeasureString(HighlightText, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize();
int startWidth = 2;
int position = e.Inner.Style.Text.IndexOf(highlightText);
if (position > 0)
{
string str = e.Inner.Style.Text.Substring(0, position);
startWidth = g.MeasureString(str, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize().Width;
Rectangle rect = new Rectangle(e.Inner.ClipBounds.X + startWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height);
using (Brush br = new SolidBrush(highlightColor))
if (style.TableCellIdentity.Column.Name == ColumnName)
e.Inner.Graphics.FillRectangle(br, rect);
}
else
{
Rectangle rect = new Rectangle(e.Inner.ClipBounds.X + startWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height);
using (Brush br = new SolidBrush(highlightColor))
if (style.TableCellIdentity.Column.Name == ColumnName)
e.Inner.Graphics.FillRectangle(br, rect);
}
}
}
}
VB
Private Sub HighlightLeftAlignedText(g As Graphics, highlightText As String, highlightColor As Color, e As GridTableControlDrawCellBackgroundEventArgs)
Dim value As Integer
Dim doubleValue As Double
Dim style As GridTableCellStyleInfo = TryCast(e.Inner.Style, GridTableCellStyleInfo)
If Not Integer.TryParse(e.Inner.Style.Text.ToString(), value) AndAlso Not Double.TryParse(e.Inner.Style.Text, doubleValue) Then
If Not String.IsNullOrEmpty(highlightText) AndAlso e.Inner.Style.Text.Contains(highlightText) AndAlso Not (e.Inner.Style.WrapText OrElse e.Inner.Style.AllowEnter) Then
Dim s = WinFormsUtils.MeasureSampleWString(g, e.Inner.Style.GdipFont)
Dim textSize As Size = g.MeasureString(highlightText, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize()
Dim startWidth As Integer = 2
Dim position As Integer = e.Inner.Style.Text.IndexOf(highlightText)
If position > 0 Then
Dim str As String = e.Inner.Style.Text.Substring(0, position)
startWidth = g.MeasureString(str, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize().Width
End If
Dim rect As New Rectangle(e.Inner.ClipBounds.X + startWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height)
Using br As New SolidBrush(highlightColor)
If style.TableCellIdentity.Column.Name = ColumnName Then
e.Inner.Graphics.FillRectangle(br, rect)
End If
End Using
End If
End If
End Sub
Highlight
the Center Aligned Text
C#
private void HighlightCenterAlignedText (Graphics g, string highlightText, Color highlightColor, GridTableControlDrawCellBackgroundEventArgs e)
{
int value;
double doubleValue;
GridTableCellStyleInfo style = e.Inner.Style as GridTableCellStyleInfo;
if (!int.TryParse(e.Inner.Style.Text.ToString(), out value) && !double.TryParse(e.Inner.Style.Text, out doubleValue))
{
if (!string.IsNullOrEmpty(highlightText) && e.Inner.Style.Text.Contains(highlightText) && !(e.Inner.Style.WrapText || e.Inner.Style.AllowEnter))
{
var s = WinFormsUtils.MeasureSampleWString(g, e.Inner.Style.GdipFont);
Size textSize = g.MeasureString(HighlightText, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize();
Size cellTextSize = g.MeasureString(e.Inner.Style.Text, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize();
int startWidth = 2;
int startEmptyWidth = (e.Inner.ClipBounds.Width - cellTextSize.Width) / 2;
int position = e.Inner.Style.Text.IndexOf(highlightText);
if (position > 0)
{
string str = e.Inner.Style.Text.Substring(0, position);
startWidth = g.MeasureString(str, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize().Width;
Rectangle rect = new Rectangle(e.Inner.ClipBounds.X + startWidth + startEmptyWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height);
using (Brush br = new SolidBrush(highlightColor))
if (style.TableCellIdentity.Column.Name == ColumnName)
e.Inner.Graphics.FillRectangle(br, rect);
}
else
{
Rectangle rect = new Rectangle(e.Inner.ClipBounds.X + startWidth + startEmptyWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height);
using (Brush br = new SolidBrush(highlightColor))
if (style.TableCellIdentity.Column.Name == ColumnName)
e.Inner.Graphics.FillRectangle(br, rect);
}
}
}
}
VB
Private Sub HighlightCenterAlignedText(g As Graphics, highlightText As String, highlightColor As Color, e As GridTableControlDrawCellBackgroundEventArgs)
Dim value As Integer
Dim doubleValue As Double
Dim style As GridTableCellStyleInfo = TryCast(e.Inner.Style, GridTableCellStyleInfo)
If Not Integer.TryParse(e.Inner.Style.Text.ToString(), value) AndAlso Not Double.TryParse(e.Inner.Style.Text, doubleValue) Then
If Not String.IsNullOrEmpty(highlightText) AndAlso e.Inner.Style.Text.Contains(highlightText) AndAlso Not (e.Inner.Style.WrapText OrElse e.Inner.Style.AllowEnter) Then
Dim s = WinFormsUtils.MeasureSampleWString(g, e.Inner.Style.GdipFont)
Dim textSize As Size = g.MeasureString(highlightText, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize()
Dim cellTextSize As Size = g.MeasureString(e.Inner.Style.Text, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize()
Dim startWidth As Integer = 2
Dim startEmptyWidth As Integer = (e.Inner.ClipBounds.Width - cellTextSize.Width) \ 2
Dim position As Integer = e.Inner.Style.Text.IndexOf(highlightText)
If position > 0 Then
Dim str As String = e.Inner.Style.Text.Substring(0, position)
startWidth = g.MeasureString(str, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize().Width
End If
Dim rect As New Rectangle(e.Inner.ClipBounds.X + startWidth + startEmptyWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height)
Using br As New SolidBrush(highlightColor)
If style.TableCellIdentity.Column.Name = ColumnName Then
e.Inner.Graphics.FillRectangle(br, rect)
End If
End Using
End If
End If
End Sub
Highlight
the Right Aligned Text
C#
private void HighlightRightAlignedText (Graphics g, string highlightText, Color highlightColor, GridTableControlDrawCellBackgroundEventArgs e)
{
int value;
double doubleValue;
GridTableCellStyleInfo style = e.Inner.Style as GridTableCellStyleInfo;
if (!int.TryParse(e.Inner.Style.Text.ToString(), out value) && !double.TryParse(e.Inner.Style.Text, out doubleValue))
{
if (!string.IsNullOrEmpty(highlightText) && e.Inner.Style.Text.Contains(highlightText) && !(e.Inner.Style.WrapText || e.Inner.Style.AllowEnter))
{
var s = WinFormsUtils.MeasureSampleWString(g, e.Inner.Style.GdipFont);
Size textSize = g.MeasureString(HighlightText, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize();
Size cellTextSize = g.MeasureString(e.Inner.Style.Text, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize();
int startWidth = 2;
int startEmptyWidth = e.Inner.ClipBounds.Width - cellTextSize.Width;
int position = e.Inner.Style.Text.IndexOf(highlightText);
if (position > 0)
{
string str = e.Inner.Style.Text.Substring(0, position);
startWidth = g.MeasureString(str, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize().Width;
Rectangle rect = new Rectangle(e.Inner.ClipBounds.X + startWidth + startEmptyWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height);
using (Brush br = new SolidBrush(highlightColor))
if (style.TableCellIdentity.Column.Name == ColumnName)
e.Inner.Graphics.FillRectangle(br, rect);
}
else
{
Rectangle rect = new Rectangle(e.Inner.ClipBounds.X + startWidth + startEmptyWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height);
using (Brush br = new SolidBrush(highlightColor))
if (style.TableCellIdentity.Column.Name == ColumnName)
e.Inner.Graphics.FillRectangle(br, rect);
}
}
}
}
VB
Private Sub HighlightRightAlignedText(g As Graphics, highlightText As String, highlightColor As Color, e As GridTableControlDrawCellBackgroundEventArgs)
Dim value As Integer
Dim doubleValue As Double
Dim style As GridTableCellStyleInfo = TryCast(e.Inner.Style, GridTableCellStyleInfo)
If Not Integer.TryParse(e.Inner.Style.Text.ToString(), value) AndAlso Not Double.TryParse(e.Inner.Style.Text, doubleValue) Then
If Not String.IsNullOrEmpty(highlightText) AndAlso e.Inner.Style.Text.Contains(highlightText) AndAlso Not (e.Inner.Style.WrapText OrElse e.Inner.Style.AllowEnter) Then
Dim s = WinFormsUtils.MeasureSampleWString(g, e.Inner.Style.GdipFont)
Dim textSize As Size = g.MeasureString(highlightText, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize()
Dim cellTextSize As Size = g.MeasureString(e.Inner.Style.Text, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize()
Dim startWidth As Integer = 2
Dim startEmptyWidth As Integer = e.Inner.ClipBounds.Width - cellTextSize.Width
Dim position As Integer = e.Inner.Style.Text.IndexOf(highlightText)
If position > 0 Then
Dim str As String = e.Inner.Style.Text.Substring(0, position)
startWidth = g.MeasureString(str, e.Inner.Style.GdipFont, e.Inner.ClipBounds.Width, StringFormat.GenericDefault).ToSize().Width
End If
Dim rect As New Rectangle(e.Inner.ClipBounds.X + startWidth + startEmptyWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height)
Using br As New SolidBrush(highlightColor)
If style.TableCellIdentity.Column.Name = ColumnName Then
e.Inner.Graphics.FillRectangle(br, rect)
End If
End Using
End If
End If
End Sub
The
Screenshot below illustrates the highlighted text in GridGroupingControl.
Samples: