views:

50

answers:

2

How does one set text of two labels with two different cultural/region format options? For first label to be ar-EG : Arabic - Egypt and second one to be en-US : English - United States ?

This to be done for Number/Date/Time/Currency formats.

A: 

I think this maybe can solve your problem:

I needed to show different money values in two differents cultural format. So i did this right after each of the code-line asingning the value:

CultureInfo US = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = US;

// Asign your label here

CultureInfo AR = new CultureInfo("ar-EG");  
Thread.CurrentThread.CurrentCulture = AR;

//Asign label here

Just remember to add the folowing namespace to the top of your code-file:

using System.Threading;
using System.Globalization;

and to re-set the previous culture. You can even override the System culture info just by adding those lines on the program.cs

josecortesp
This doesn't work in case of Number, it always show US format?
Ahmed
Actually, it should do, it works with me(using es-CR and en-US). Remember that code gets the cultural config from the OS, so if you have configured your "ar-EG" diferently as it should be, it will take that paramethers to format your currency/dates and so on...
josecortesp
+1  A: 

Use the culture explicitly in the ToString() method. For example:

  DateTime dt = DateTime.Now;
  CultureInfo arabic = CultureInfo.GetCultureInfo("ar-EG");
  label1.Text = dt.ToString(arabic.DateTimeFormat);
  CultureInfo english = CultureInfo.GetCultureInfo("en-US");
  label2.Text = dt.ToString(english.DateTimeFormat);

Use CultureInfo.NumberFormat to format numbers.

Hans Passant