You need to get the width of the string using the Graphics.MeasureString Method
Here the simple example, hint, the width is depending of the size of the font not the font-size of your GroupBox
property.
SizeF stringSize = new SizeF();
private void groupBox1_Paint(object sender, PaintEventArgs e)
{
string measureString = "this is your text";
Font stringFont = new Font("Arial", 17);
// Measure string.
stringSize = e.Graphics.MeasureString(measureString, stringFont);
}
private void button1_Click(object sender, EventArgs e)
{
groupBox1.Text = "this is your text";
groupBox1.Width = (int)stringSize.Width;
}
I hope it will helps you.