Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a button depending on its text

In the process of translating an application with C# + Winforms, I need to change a button's text depending on the language.

My problem is the following :

Let's say I want to translate a button from "Hi all!" to "Bonjour tout le monde" !

As you can guess, the button's size won't be the same if I enter english text or french one... My question is "simple", how can I manage to resize the button on the fly so the text fits its content in the button ?

So far I got something like that !

[Hi all!]

[Bonjour]

like image 511
Andy M Avatar asked Oct 19 '10 06:10

Andy M


People also ask

How do you make a button expand with text?

Set a padding to the container, it will make it larger depending on how much text is added. For example: padding: 4px 10px; So in your case, remove the width from your <a> selector and add the padding.

How do I fix a button size in HTML?

Tip: Use pixels if you want to set a fixed width and use percent for responsive buttons (e.g. 50% of its parent element).


1 Answers

There's absolutely no need to use the underlying Graphics object as the other posters have said.

If you set the button's AutoSize property to true, the AutoSizeMode to GrowAndShrink, and the AutoEllipsis to false, it will resize automatically to fit the text.

That being said, you may need to make several layout adjustments to make this change fit into your UI. You can adjust the button's padding to add space around the text, and you may want to place your buttons in a TableLayoutPanel (or something) to stop them from overlapping when they resize.

Edit: @mastro pointed out that: AutoEllipsis is only valid when AutoSize is false (As explained in the documentation), so it can be safely ignored as long as the other three properties are set correctly.

like image 195
Andrew Hanlon Avatar answered Oct 07 '22 01:10

Andrew Hanlon