471,320 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Re: Formatting numeric data in textbox

I need to format any decimal value so that it is right aligned, thousands
are separated and shows 2 decimal places always.
It is good to show the info in the format the user prefers.
That is language/locale speciffic (CultureInfo in the .NET lingo)
So you should use something like ToString("N")
If you are sure you want 2 digits after decimal separator, then use "N2"

--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #1
8 6378
Mihai,
It is good to show the info in the format the user prefers.
That is language/locale speciffic (CultureInfo in the .NET lingo)
So you should use something like ToString("N")
If you are sure you want 2 digits after decimal separator, then use "N2"
Thank you.

I need to format numbers when form is initially shown and after user leaves
changed TextBox.
Which TextBox event should be used for this ? Where ToString("N2") call
should be placed ?

Andrus.

Jun 27 '08 #2
I need to format numbers when form is initially shown and after user leaves
changed TextBox.
Which TextBox event should be used for this ? Where ToString("N2") call
should be placed ?
I personally try to stay away from Mask.
I initialize the fields when I create the form (using ToString),
and I validate them when user tries to submit the data
(using Parse or TryParse)
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #3
I personally try to stay away from Mask.
I initialize the fields when I create the form (using ToString),
and I validate them when user tries to submit the data
(using Parse or TryParse)
I do'nt understant this.
In the sample code I posted Textbox is data bound to decimal property Amount
which has value 12
Winforms initializes this TextBox automatically.

How to force Textbox to show 12.00 when form is shown and TextBox does not
receive focus ?

DataGridView column has DefaultCellStyle.Format property for this.
I havent found .Format property when TextBox is not in DataGridView.

Andrus.

Jun 27 '08 #4
In the sample code I posted Textbox is data bound to decimal property
Amount which has value 12
Winforms initializes this TextBox automatically.
I am not sure if Winforms uses the CultureInfo for that (to use the proper
decimal/thousand separators).
If it does, then just let Winforms do the work.

Otherwise, you might have to capture the loss of focus and reformat the way
you want.

Personally (as user), I don't care much about stuff being reformated.
If I type .2 and remains like this, I'm ok with it.
But I do care a lot about separators (123,456 means a completely different
thing in Germany vs US)

--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #5
>In the sample code I posted Textbox is data bound to decimal property
>Amount which has value 12
Winforms initializes this TextBox automatically.

I am not sure if Winforms uses the CultureInfo for that (to use the proper
decimal/thousand separators).
If it does, then just let Winforms do the work.
The issue is not related to cultureinfo.
Isse is that it is not possible to show 12 with 2 decimal places when form
is opened.
Otherwise, you might have to capture the loss of focus and reformat the
way
you want.

Personally (as user), I don't care much about stuff being reformated.
If I type .2 and remains like this, I'm ok with it.
But I do care a lot about separators (123,456 means a completely different
thing in Germany vs US)
My issue is that my program calculates product cost etc. using 5 digits
after comma and this must remain.
Users want to see this value with 2 digits after comma and also to enter
this value.
DataGridView DefaultCellStyle has Format property which allows this but
there is no such property when TextBox is placed to form.

Andrus.

Jun 27 '08 #6
The issue is not related to cultureinfo.
Not quite. CultureInfo gives you the right decimal separator
(which is a good thing).
Isse is that it is not possible to show 12 with 2 decimal places when form
is opened.
ToString("N2")
DataGridView DefaultCellStyle has Format property which allows this but
there is no such property when TextBox is placed to form.
Than you have to do it yourself by calling ToString when the control looses
focus.
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #7
Mihai,
>DataGridView DefaultCellStyle has Format property which allows this but
there is no such property when TextBox is placed to form.
Than you have to do it yourself by calling ToString when the control
looses
focus.
When form is initially shown, control does not lose focus.
So there is no place to call ToString().

Andrus.

Jun 27 '08 #8
When form is initially shown, control does not lose focus.
So there is no place to call ToString().
Then you might have to do the same at creation,
not only at lose focus.
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mike P | last post: by
2 posts views Thread by John A Grandy | last post: by
1 post views Thread by Jaros³aw Kozik | last post: by
8 posts views Thread by Frank Swarbrick | last post: by
reply views Thread by rosydwin | last post: by

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.