473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Invariant US format in NumericUpDown

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
Oct 23 '08 #1
7 1850
On Wed, 22 Oct 2008 17:24:08 -0700, pamela fluente
<pa***********@libero.itwrote:
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'm not sure you can. You could sub-class NumericUpDown, and override
UpdateEditText() and ValidateEditText(). But ParseEditText() isn't
virtual, so I'm not sure you can completely override the built-in behavior.

That said, I think as a user I'd be at least somewhat annoyed if an
application ignored my current culture settings. I hope you have a
_really_ good reason for doing that.

Pete
Oct 23 '08 #2

"pamela fluente" wrote:
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

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 23 '08 #3
On 23 Ott, 07:48, Morten Wennevik [C# MVP]
<MortenWenne...@hotmail.comwrote:
"pamela fluente" wrote:
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 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
Oct 23 '08 #4

"pamela fluente" wrote:
On 23 Ott, 07:48, Morten Wennevik [C# MVP]
<MortenWenne...@hotmail.comwrote:
"pamela fluente" wrote:
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

--
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; }
--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 23 '08 #5
>
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; }
--
Happy Coding!
Morten Wennevik [C# MVP]- Nascondi testo citato
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

Oct 23 '08 #6
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
"pamela fluente" <pa***********@libero.itschreef in bericht
news:e9**********************************@y29g2000 hsf.googlegroups.com...
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

Oct 24 '08 #7
On 24 Ott, 13:13, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
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
Oct 24 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
10
by: Bonj | last post by:
I'm using a number of NumericUpDown controls on my form, which controls printer settings such as number of pages. However I want to override one of the default behaviours of the control, namely...
0
by: Koert | last post by:
Hi In the NumericUpDown control I have to detect that Tab was pressed so I have inherited the NumericUpDown and overroden the PreProcessMessage function. In the DateTimePicker (for example) this...
4
by: Art | last post by:
Hi, I have a MyTextBox class that inherits from TextBox. How do I make it display the information as, say 0.00. Sometimes the value will be typed by the user. Other times the value will be...
3
by: abc my vclass | last post by:
My win-form have many numericupdown controls to applied. But numericupdown control don't like textbox, text box control can automatic selected text when got focus. Is there any method can let me...
4
by: illegal.prime | last post by:
Hey all, I have a NumericUpDown control on a Panel or potentially a Form that have AutoScroll enabled. When the NumericUpDown is active I would like the Form/Panel/whatever not to scroll when I...
5
by: Len Weltman | last post by:
I am trying to pass a NumericUpDown object into a class method using Visual Studio 2005, but the control type is not found in Intellisense and the type declaration is flagged as an error. Here...
2
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hello experts, I posted a disturbing question in here:...
2
by: helveticus | last post by:
Is there a way to keep datetime expressions culture invariant? My app is culture dependent and contains a series of datetime settings that are saved in cookies. The cookie date strings raise...
7
by: pamela fluente | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.