)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to highlight the given search text in consideration with text alignment in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: January 12, 2017 |
Last Revised Date: December 23, 2019

Highlight text

In order to highlight the search text in 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 void HighlightLeftAlignedText(Graphics g, String highlightText, Color highlightColor,GridTableControlDrawCellBackgroundEventArgs e)
    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
               Dim rect As New Rectangle(e.Inner.ClipBounds.X + startWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height)
               Using br As Brush = New SolidBrush(highlightColor)
                    If style.TableCellIdentity.Column.Name = ColumnName Then
                        e.Inner.Graphics.FillRectangle(br, rect)
                   End If
                End Using
            Else
                Dim rect As New Rectangle(e.Inner.ClipBounds.X + startWidth, e.Inner.ClipBounds.Y, textSize.Width - 2, e.Inner.ClipBounds.Height)
                Using br As Brush = 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 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 void HighlightCenterAlignedText (Graphics g, String highlightText, Color highlightColor, GridTableControlDrawCellBackgroundEventArgs e)
    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
                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 Brush = New SolidBrush(highlightColor)
                    If style.TableCellIdentity.Column.Name = ColumnName Then
                            e.Inner.Graphics.FillRectangle(br, rect)
                    End If
                End Using
            Else
                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 Brush = 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 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 void HighlightRightAlignedText (Graphics g, String highlightText, Color highlightColor, GridTableControlDrawCellBackgroundEventArgs e)
    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
                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 Brush = New SolidBrush(highlightColor)
                    If style.TableCellIdentity.Column.Name = ColumnName Then
                            e.Inner.Graphics.FillRectangle(br, rect)
                    End If
                End Using
            Else
                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 Brush = 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 If
End Sub

 

Screenshot

Highlight the search text

 

Samples:

C#: HighLight Text CS

VB: HighLight Text VB

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon