dynamic button width

  • Thread starter Thread starter TS
  • Start date Start date
T

TS

I have an app that supports multiple languages. At run time I have to set
the text of the buttons to the currect language. I need whatever language's
text to fit the size of the button. I don't want to have to make the button
really wide to try to guess the maximum width necessary for every language I
support.

Is there a way to allow the button to resize it's width based on the text it
has?
 
This is exactly the problem that the Windows Forms localization features are
designed to solve. While there's more to it than I can reasonably put into a
newsgroup post, here's the essential part:

You build your form's UI in your default language (US english by default in
the US).

Set your form's Localizable property to True. This basically 'turns on' the
localizable feature, so that the following has an effect.

Set the form's Language property to some other language that you want to
support, and make any changes you need to make in the form's UI: replace the
English text with text in the target language, resize the controls to make
them large enough to contain the text, make any other changes in layout on
the form that you need, including resizing the whole form if necessary to
make things fit.

Repeat this for any other languages you want to support. Resource files for
each language are created and populated by VS.Net, containing both the text
and layout information. You can see these files by clicking the 'show all
files' button in the solution explorer, and then expanding the files below
the form file.

When the application starts up, your code can either use the machine's
current UICulture, or have the user choose a language from your list of
supported languages, whereupon your code explicitly sets the UICulture to
suit. Microsoft recommends the latter, since it's possible that the machine
may not be set by default to the culture of the actual user who sits down at
the machine. When the application sees a UICulture that's different from the
default, the application's resources are taken from the language-specific
resource file instead of the default, and the form is displayed according to
the design you did for that culture.

In addition to the UICulture, which controls the display of UI resources,
there's also a Culture, which is used to control how formatting of currency
and so forth are handled by methods like String.Format(). Both of these
features together allow you to provide localized applications for other
cultures with relative ease.

There's way more about this in the MSDN library; look around there or find a
good book on Windows Forms programming. I have Windows Forms Programming in
C# by Chris Sells, and it has a small but good discussion of doing this
localization using both VS.Net and the winres.exe program. If you happen to
have Microsoft's self-paced training materials for the MCSD tests, you'll
find labs that exercise this feature. And I wouldn't be surprised if you can
find a localization tutorial in the VS.Net or .Net frameworks docs, too.

Whatever you do, I'd recommend that you do it using the built-in
localization capabilities, rather than doing a home-grown solution of your
own. Only where the built-in features can't be made to work for you on
account of specialized requirements should you roll your own.

HTH,
Tom Dacon
Dacon Software Consulting
 
Back
Top