Invariant US format in NumericUpDown

P

pamela fluente

My numericUpDowns show numbers in the format 1.500,56 (italy).

Instead, I need *invariantly* that they show and accept the format
1,500.56, as in the USA.

What's the right way to do that? I have tried to set the current
culture to
the US culture, but nothing changed.

For instance I tried without success (vb) :
Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")

Both a vb or c# example will be very useful.

Thanks a lot.


-P
 
M

Morten Wennevik [C# MVP]

pamela fluente said:
My numericUpDowns show numbers in the format 1.500,56 (italy).

Instead, I need *invariantly* that they show and accept the format
1,500.56, as in the USA.

What's the right way to do that? I have tried to set the current
culture to
the US culture, but nothing changed.

For instance I tried without success (vb) :
Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")

Both a vb or c# example will be very useful.

Thanks a lot.


-P

Hi Pamela,

Setting Application.CultureInfo will change the culture for the entire
application. NumericUpDown will display 1,500.56 (if you set it after
InitializeComponent it will initially display 1.500,56 and switch format when
you start using it). If you set it as part of an inherited usercontrol you
may even mess up the Visual Studio designer and need a restart to be able to
reset the culture.

As Peter pointed out, forcing a culture is rarely a good idea, if ever, and
shouldn't be necessary either as it is only the displayed format that
changes. Internally it doesn't matter if the decimal is ',' or the
thousandseparator is. The only time I can think it may be an issue if you in
some way use the Text property of the NumericUpDown instead of its Value
 
P

pamela fluente

pamela fluente said:
My numericUpDowns show numbers in the format 1.500,56 (italy).
Instead, I need *invariantly* that they show and accept the format
1,500.56, as in the USA.
What's the right way to do that?  I have tried to set the current
culture to
the US culture, but nothing changed.
For instance I tried  without success (vb) :
 Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")
Both a vb or c#  example will be very useful.
Thanks a lot.

Hi Pamela,

Setting Application.CultureInfo will change the culture for the entire
application.  NumericUpDown will display 1,500.56 (if you set it after
InitializeComponent it will initially display 1.500,56 and switch format when
you start using it).  If you set it as part of an inherited usercontrolyou
may even mess up the Visual Studio designer and need a restart to be ableto
reset the culture.

As Peter pointed out, forcing a culture is rarely a good idea, if ever, and
shouldn't be necessary either as it is only the displayed format that
changes.  Internally it doesn't matter if the decimal is ',' or the
thousandseparator is.  The only time I can think it may be an issue if you in
some way use the Text property of the NumericUpDown instead of its Value

--
Happy Coding!
Morten Wennevik [C# MVP]- Nascondi testo citato

- Mostra testo citato -



Yes, the reason is that this is a trading application on the NYSE,
AMEX, etc. and working with dollars.

I need to be consistent and use always the same format. I have noticed
that several time the user here typed numbers as 1000.23 $ for
instance (getting the wrong input)
it's rare that someone does 1000,23 $ because they are used to see the
US notation in stock market or exchanges.

In my case it would be very nice that the application takes and
displays the numbers
in the same way everywhere.


I have not tried Setting Application.CultureInfo before
InitializeComponent. I will try.

Let me know please if you see a way to do this the right way.


-P
 
M

Morten Wennevik [C# MVP]

pamela fluente said:
pamela fluente said:
My numericUpDowns show numbers in the format 1.500,56 (italy).
Instead, I need *invariantly* that they show and accept the format
1,500.56, as in the USA.
What's the right way to do that? I have tried to set the current
culture to
the US culture, but nothing changed.
For instance I tried without success (vb) :
Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")
Both a vb or c# example will be very useful.
Thanks a lot.

Hi Pamela,

Setting Application.CultureInfo will change the culture for the entire
application. NumericUpDown will display 1,500.56 (if you set it after
InitializeComponent it will initially display 1.500,56 and switch format when
you start using it). If you set it as part of an inherited usercontrol you
may even mess up the Visual Studio designer and need a restart to be able to
reset the culture.

As Peter pointed out, forcing a culture is rarely a good idea, if ever, and
shouldn't be necessary either as it is only the displayed format that
changes. Internally it doesn't matter if the decimal is ',' or the
thousandseparator is. The only time I can think it may be an issue if you in
some way use the Text property of the NumericUpDown instead of its Value

--
Happy Coding!
Morten Wennevik [C# MVP]- Nascondi testo citato

- Mostra testo citato -



Yes, the reason is that this is a trading application on the NYSE,
AMEX, etc. and working with dollars.

I need to be consistent and use always the same format. I have noticed
that several time the user here typed numbers as 1000.23 $ for
instance (getting the wrong input)
it's rare that someone does 1000,23 $ because they are used to see the
US notation in stock market or exchanges.

In my case it would be very nice that the application takes and
displays the numbers
in the same way everywhere.


I have not tried Setting Application.CultureInfo before
InitializeComponent. I will try.

Let me know please if you see a way to do this the right way.


-P

In this case I think I would have used a regular TextBox and handled the
DataBinding format and parse events and possibly created my own up and down
buttons, or created a UserControl containing of a NumericUpDown control with
a TextBox control on top.

Using a regular TextBox

TextBox tb = new TextBox();
Controls.Add(tb);

BindingSource bs = new BindingSource(this, "");

Binding binding = new Binding("Text", bs, "Value", true,
DataSourceUpdateMode.OnPropertyChanged);
binding.Format += new ConvertEventHandler(binding_Format);
binding.Parse += new ConvertEventHandler(binding_Parse);
tb.DataBindings.Add(binding);
}

void binding_Parse(object sender, ConvertEventArgs e)
{
string s = (string)e.Value;
e.Value = decimal.Parse(s, CultureInfo.GetCultureInfo("en-US"));
}

void binding_Format(object sender, ConvertEventArgs e)
{
decimal d = (decimal)e.Value;
e.Value = d.ToString("N2", CultureInfo.GetCultureInfo("en-US"));
}

public decimal Value { get; set; }
 
P

pamela fluente

In this case I think I would have used a regular TextBox and handled the
DataBinding format and parse events and possibly created my own up and down
buttons, or created a UserControl containing of a NumericUpDown control with
a TextBox control on top.

Using a regular TextBox

            TextBox tb = new TextBox();
            Controls.Add(tb);

            BindingSource bs = new BindingSource(this, "");

            Binding binding = new Binding("Text", bs, "Value", true,
DataSourceUpdateMode.OnPropertyChanged);
            binding.Format += new ConvertEventHandler(binding_Format);
            binding.Parse += new ConvertEventHandler(binding_Parse);
            tb.DataBindings.Add(binding);
        }

        void binding_Parse(object sender, ConvertEventArgs e)
        {
            string s = (string)e.Value;
            e.Value = decimal.Parse(s, CultureInfo.GetCultureInfo("en-US"));
        }

        void binding_Format(object sender, ConvertEventArgs e)
        {
            decimal d = (decimal)e.Value;
            e.Value = d.ToString("N2", CultureInfo.GetCultureInfo("en-US"));
        }

        public decimal Value { get; set; }

Thanks a lot Morten for your code and kindness.

I was actually hoping there was a simpler way to tell a control to
show and accept numbers in the US format. But ... I guess I need to go
your way!

I will try that. Thanks a Lot. Very helpful.

-P
 
C

Cor Ligthert[MVP]

Pamela,



Strange as I am as well in a non English European Language Culture

Does this show to me

Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")
MessageBox.Show(1000.1.ToString)

1000.1

Are you sure you are not filling the combobox with strings?

Cor
 
P

pamela fluente

Pamela,

Strange as I am as well in a non English European Language Culture

Does this show to me

Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")
MessageBox.Show(1000.1.ToString)

1000.1

Are you sure you are not filling the combobox with strings?

Cor

Thank you Cor, Actually now that you say that, I have tried a little
experiment (code below)
and it seems to be working (?).

Now I want to go back to my original code and see why it was not
working there ...

-P

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim f As New Form1
Dim n As New NumericUpDown
n.DecimalPlaces = 2
n.ThousandsSeparator = True
f.Controls.Add(n)

n.Maximum = 100000000
n.Value = 1000.1
f.Show()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Application.CurrentCulture =
Globalization.CultureInfo.GetCultureInfo("en-US")
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top