How do I autosize a button to fit its text

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

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页