Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the color of the border on a group box?

In C#.NET I am trying to programmatically change the color of the border in a group box.

Update: This question was asked when I was working on a winforms system before we switched to .NET.

like image 709
Amy Patterson Avatar asked Sep 16 '08 20:09

Amy Patterson


People also ask

How do I make my group box border invisible?

You can use panel and set its Border to none. In case you still want to use groupbox, . Net do not provide border style property.


2 Answers

Just add paint event.

    private void groupBox1_Paint(object sender, PaintEventArgs e)     {         GroupBox box = sender as GroupBox;         DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue);     }       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(this.BackColor);              // 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));         }     } 
like image 132
user1944617 Avatar answered Sep 23 '22 19:09

user1944617


Building on the previous answer, a better solution that includes the label for the group box:

groupBox1.Paint += PaintBorderlessGroupBox;  private void PaintBorderlessGroupBox(object sender, PaintEventArgs p) {   GroupBox box = (GroupBox)sender;   p.Graphics.Clear(SystemColors.Control);   p.Graphics.DrawString(box.Text, box.Font, Brushes.Black, 0, 0); } 

You might want to adjust the x/y for the text, but for my use this is just right.

like image 43
Mick Bruno Avatar answered Sep 22 '22 19:09

Mick Bruno