title: Custom GroupBox with custom TextColor, BorderColor and Transparent BackColor
body: <p>I'm using <code>WinForms</code>. In my form I have a <code>GroupBox</code>. This is a <strong><em>custom</em></strong> group box. I wanted a transparent background for the <code>groupbox</code>. I'm having issues creating a transparent background for the <code>groupbox</code> The problem with this code is i keep on getting an error when i set the group box <code>backcolor</code> to transparent. </p>
  
  <blockquote>
    <p>Error: Control does not support transparent background colors. </p>
  </blockquote>
  
  <p><code>g.Clear(BackColor = Color.Transparent);</code> (This is the line that is giving me the problem)</p>
  
  <pre><code>    private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor)
      {
          if (box != null)
          {
              Brush textBrush = new SolidBrush(textColor);
              Brush borderBrush = new SolidBrush(borderColor);
              Pen borderPen = new Pen(borderBrush);
              SizeF strSize = g.MeasureString(box.Text, box.Font);
              Rectangle rect = new Rectangle(box.ClientRectangle.X,
                                             box.ClientRectangle.Y + (int)(strSize.Height / 2),
                                             box.ClientRectangle.Width - 1,
                                             box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1);
  
              // Clear text and border
              g.Clear(BackColor = Color.Transparent);
  
              // Draw text
              g.DrawString(box.Text, box.Font, textBrush, box.Padding.Left, 0);
  
              // Drawing Border
              //Left
              g.DrawLine(borderPen, rect.Location, new Point(rect.X, rect.Y + rect.Height));
              //Right
              g.DrawLine(borderPen, new Point(rect.X + rect.Width, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height));
              //Bottom
              g.DrawLine(borderPen, new Point(rect.X, rect.Y + rect.Height), new Point(rect.X + rect.Width, rect.Y + rect.Height));
              //Top1
              g.DrawLine(borderPen, new Point(rect.X, rect.Y), new Point(rect.X + box.Padding.Left, rect.Y));
              //Top2
              g.DrawLine(borderPen, new Point(rect.X + box.Padding.Left + (int)(strSize.Width), rect.Y), new Point(rect.X + rect.Width, rect.Y));
          }
      }
  
      private void groupBox1_Paint(object sender, PaintEventArgs e)
      {
          GroupBox box = sender as GroupBox;
          DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue);
      }
  </code></pre>
  
  <p><code>g.Clear(groupBox1.BackColor = Color.Transparent);</code></p>
  
  <p>If i do this i get: </p>
  
  <blockquote>
    <p>This example consists of a <code>Panel</code> with a dice image inside the panel,
    and the custom <code>Groupbox</code>.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/9k5hr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9k5hr.png" alt="enter image description here"></a></p>