Initialize TextBox contents for the computer’s locale in C#

[locale]

Suppose you place numeric, currency, date, and time values in TextBox controls at run time. At design time, the program may have trouble reading those values if the computer running the program is not using the same locale that you used to write the program. For example, suppose you place the value 3.14 in a TextBox and then the program tries to parse that value. On a computer running in a United States locale, the program parses the value with no problem. However, German locales use a comma for a decimal separator, so if you run the program on a computer that uses a German locale, the program throws an exception.

One way you can avoid that problem is to use simply leave the TextBox values blank and make the user fill them in using the computer’s locale formats.

If you want to present initial values, you can also use the form’s Load event handler to initialize TextBox values. As long as you use locale-aware methods to format the values, then their format should match the one used by the computer’s locale so the program can later parse the values.

This example uses the following code to display float, currency, date, and time values.

// Set initial TextBox values.
private void Form1_Load(object sender, EventArgs e)
{
    // Uncomment to test for a German locale.
    //CultureInfo culture_info = new CultureInfo("DE-de");
    //Thread.CurrentThread.CurrentCulture = culture_info;

    // Set the TextBox values.
    txtFloat.Text = (3.14).ToString();
    txtCurrency.Text = (13.37).ToString("C");
    txtDate.Text = DateTime.Today.ToShortDateString();
    txtTime.Text = DateTime.Now.ToShortTimeString();
}

The commented code sets the computer’s locale to generic German so you can test the program in that locale. The code then places values in the form’s TextBox controls. Those statements automatically use the appropriate locale so the program can later parse the values.

When you click the program’s Parse button, the following code parses the values and displays the results in the Console window.

// Parse the values.
private void btnParse_Click(object sender, EventArgs e)
{
    float number = float.Parse(txtFloat.Text);
    decimal currency = decimal.Parse(txtCurrency.Text, NumberStyles.Any);
    DateTime date = DateTime.Parse(txtDate.Text);
    DateTime time = DateTime.Parse(txtTime.Text);

    Console.WriteLine(number);
    Console.WriteLine(currency.ToString("C"));
    Console.WriteLine(date.ToShortDateString());
    Console.WriteLine(time.ToShortTimeString());
}

This code simply parses the values in the TextBox controls. The Parse methods automatically use the computer’s locale.

Download the example to experiment with it. For example, you can test it in other locales.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in formatting, internationalization, localization and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.