)
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 achieve the GroupBox appearance in Gradient panel?

Platform: WinForms |
Control: GradientPanel |
Published Date: February 15, 2016 |
Last Revised Date: May 24, 2019

GroupBox appearance can be achieved by creating the custom control from the Gradient Panel.

 

The following code example demonstrates the same.

C#

/// <summary>
/// Overrides the paint event.
/// </summary>
/// <param name="e">The PaintEventArgs contains the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    PaintBackGround(e.Graphics);
    PaintHeaderText(e.Graphics);
}
 
/// <summary>
/// This method helps paint the title of the GradientPanelAdv.
/// </summary>
/// <param name="g">The Graphics object for paint event handler.</param>
private void PaintHeaderText(System.Drawing.Graphics g)
{
    //To check whether if string has something
    if (this.HeaderText == string.Empty) { return; }
 
    //To set graphics smoothing mode to Anit-Alias 
    g.SmoothingMode = SmoothingMode.AntiAlias;
 
    //To declare the variables
    SizeF StringSize = g.MeasureString(this.HeaderText, this.Font);
    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
    System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
    System.Drawing.Brush BackgroundBrush = new SolidBrush(this.BackColor);
    System.Drawing.SolidBrush TextColorBrush = new SolidBrush(this.ForeColor);
           
    path.AddRectangle(new Rectangle(this.ClientRectangle.X + 20, this.ClientRectangle.Y + 5, (int)Math.Ceiling(StringSize.Width) + 10, (int)Math.Ceiling(StringSize.Height)));
            
    if (this.BackgroundColor.GradientStyle == GradientStyle.None)
    {
        //To paint text rectangle
        g.FillPath(BackgroundBrush, path);
    }
            
    //To draw the header text
    g.DrawString(this.HeaderText, this.Font, TextColorBrush, 26, 5);
 
    // To destroy Graphic Objects
    if (path != null) { path.Dispose(); }
    if (BorderBrush != null) { BorderBrush.Dispose(); }
    if (BackgroundBrush != null) { BackgroundBrush.Dispose(); }
    if (TextColorBrush != null) { TextColorBrush.Dispose(); }
}
 
/// <summary>
/// To paint the background of the GradientPanelExt.
/// </summary>
/// <param name="g">The Graphics object for paint event handler.</param>
private void PaintBackGround(System.Drawing.Graphics g)
{
    //To set Graphics smoothing mode to Anit-Alias
    g.SmoothingMode = SmoothingMode.AntiAlias;
 
    //To declare Variables
    int ArcWidth = 20; int ArcHeight = 20;
    int ArcX1 = 0; int ArcX2 = this.Width - (ArcWidth + 1);
    int ArcY1 = 10; int ArcY2 = this.Height - (ArcHeight + 1);
 
    System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
    System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
    System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness);
            
    //To create rounded rectangle path
    graphicsPath.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, 90); // Top Left
    graphicsPath.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, 90); //Top Right
    graphicsPath.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, 90); //Bottom Right
    graphicsPath.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, 90); //Bottom Left
    graphicsPath.CloseAllFigures();
            
    //To draw a border
    g.DrawPath(BorderPen, graphicsPath);
 
    //To destroy Graphic Objects
    if (graphicsPath != null) { graphicsPath.Dispose(); }
    if (BorderBrush != null) { BorderBrush.Dispose(); }
    if (BorderPen != null) { BorderPen.Dispose(); }
}

 

VB

''' <summary>
''' Overrides the paint event
''' </summary>
''' <param name="e"> The PaintEventArgs contains the event data.</param>
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
 MyBase.OnPaint(e)
 PaintBackGround(e.Graphics)
 PaintHeaderText(e.Graphics)
End Sub
 
''' <summary>
''' This method helps paint the title of the GradientPanelAdv.
''' </summary>
''' <param name="g">The Graphics object for paint event handler.</param>
Private Sub PaintHeaderText(ByVal g As System.Drawing.Graphics)
 'To check whether if string has something
 If Me.HeaderText = String.Empty Then
  Return
 End If
 
 'To set graphics smoothing mode to Anit-Alias 
 g.SmoothingMode = SmoothingMode.AntiAlias
 
 'To declare the variables
 Dim StringSize As SizeF = g.MeasureString(Me.HeaderText, Me.Font)
 Dim path As New System.Drawing.Drawing2D.GraphicsPath()
 Dim BorderBrush As System.Drawing.Brush = New SolidBrush(Me.BorderColor)
 Dim BackgroundBrush As System.Drawing.Brush = New SolidBrush(Me.BackColor)
 Dim TextColorBrush As System.Drawing.SolidBrush = New SolidBrush(Me.ForeColor)
 
 path.AddRectangle(New Rectangle(Me.ClientRectangle.X + 20, Me.ClientRectangle.Y + 5, CInt(Fix(Math.Ceiling(StringSize.Width))) + 10, CInt(Fix(Math.Ceiling(StringSize.Height)))))
 
 If Me.BackgroundColor.GradientStyle = GradientStyle.None Then
  'To paint text rectangle
  g.FillPath(BackgroundBrush, path)
 End If
 
 'To draw the header text
 g.DrawString(Me.HeaderText, Me.Font, TextColorBrush, 26, 5)
 
 ' To destroy Graphic Objects
 If path IsNot Nothing Then
  path.Dispose()
 End If
 If BorderBrush IsNot Nothing Then
  BorderBrush.Dispose()
 End If
 If BackgroundBrush IsNot Nothing Then
  BackgroundBrush.Dispose()
 End If
 If TextColorBrush IsNot Nothing Then
  TextColorBrush.Dispose()
 End If
End Sub
 
''' <summary>
''' To paint the background of the GradientPanelExt.
''' </summary>
''' <param name="g">The Graphics object for paint event handler.</param>
Private Sub PaintBackGround(ByVal g As System.Drawing.Graphics)
 'To set Graphics smoothing mode to Anit-Alias
 g.SmoothingMode = SmoothingMode.AntiAlias
 
 'To declare Variables
 Dim ArcWidth As Integer = 20
 Dim ArcHeight As Integer = 20
 Dim ArcX1 As Integer = 0
 Dim ArcX2 As Integer = Me.Width - (ArcWidth + 1)
 Dim ArcY1 As Integer = 10
 Dim ArcY2 As Integer = Me.Height - (ArcHeight + 1)
 
 Dim graphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
 Dim BorderBrush As System.Drawing.Brush = New SolidBrush(Me.BorderColor)
 Dim BorderPen As System.Drawing.Pen = New Pen(BorderBrush, Me.BorderThickness)
 
 'To create rounded rectangle path
 graphicsPath.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, 90) ' Top Left
 graphicsPath.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, 90) 'Top Right
 graphicsPath.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, 90) 'Bottom Right
 graphicsPath.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, 90) 'Bottom Left
 graphicsPath.CloseAllFigures()
 
 'To draw a border
 g.DrawPath(BorderPen, graphicsPath)
 
 'To destroy Graphic Objects
 If graphicsPath IsNot Nothing Then
  graphicsPath.Dispose()
 End If
 If BorderBrush IsNot Nothing Then
  BorderBrush.Dispose()
 End If
 If BorderPen IsNot Nothing Then
  BorderPen.Dispose()
 End If
End Sub

 

Showing customization of GradientPanel

Figure 1: GradientPanel customization.

Sample Links:

 

C#: GradientPanel_GroupBoxAppearance_C#

VB: GradientPanel_GroupBoxAppearance_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