How to change the border color of a Groupbox

Asked

Viewed 3,813 times

0

How to change the border color of a Group-box?

Because I need to put a very light background color...there the edge of the Group-box q I have almost do not appear because they are a very light gray too....

I needed to change to a darker color because I can’t change the background color..

Does anyone have any suggestions???

2 answers

1


Create the event below...

private void groupBox1_Paint(object sender, PaintEventArgs e)
    {
        GroupBox box = sender as GroupBox;
        DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue, Collor.Yellow);
    }

And the method responsible for changing the border color and text below...

private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor, Color backgroundColor)
    {
        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);

            // Coloque a cor do background aqui
            g.Clear(backgroundColor);

            // 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));
        }
    }

Change your Groupbox’s Paint event to call groupBox1_Paint.

inserir a descrição da imagem aqui

  • Good afternoon psNytrancez. The problem is that when I create this event and call the Method, it cleans the back color, the edges are the correct color but the background is gray.

  • @rafaelbertoco Alterei. Onde tem o comentário "// Put the background color here"

  • Okay, correct, it worked, thank you very much for your attention.

0

you need to create a custom control.

Create a Class in your project and use the code below:

public class MyGroupBox : GroupBox
  {
    private Color _borderColor = Color.Black;

    public Color BorderColor
    {
      get { return this._borderColor; }
      set { this._borderColor = value; }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      //get the text size in groupbox
      Size tSize = TextRenderer.MeasureText(this.Text, this.Font);

      Rectangle borderRect = e.ClipRectangle;
      borderRect.Y = (borderRect.Y + (tSize.Height / 2));
      borderRect.Height = (borderRect.Height - (tSize.Height / 2));
      ControlPaint.DrawBorder(e.Graphics, borderRect, this._borderColor, ButtonBorderStyle.Solid);

      Rectangle textRect = e.ClipRectangle;
      textRect.X = (textRect.X + 6);
      textRect.Width = tSize.Width;
      textRect.Height = tSize.Height;
      e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
      e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
    }
  }

After building the project see in your Toolbox the new component created in this example the new control will be Mygroupbox, so you will be able to access the property Bordercolor:

myGroupBox1.BorderColor = Color.Red;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.