Live Chat Icon For mobile
Live Chat Icon

How do I autosize a button to fit its text

Platform: WinForms| Category: Button

Get a Graphics object for the button and use its MeasureString method to compute the width you need.


Graphics g = button1.CreateGraphics();
float w = g.MeasureString(button1.Text, button1.Font).Width;
g.Dispose();
button1.Width = (int)w + 12; // 12 is for the margins 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.