469,917 Members | 1,834 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Formatting numbers with events

I need some code that, on each keyup event, will take all of the numbers
typed into the text box and format as they type to look like this
100
1,000
10,000
100,000
1,000,000

John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
4 2622
Well, because you are *updating* the numbers in the text-box after the user
inputs, then you'll have to remove your formatting and then reapply your
formatting to get everything to work.

using System;

public class TestFormatting {
private static void Main(string[] args) {
Console.WriteLine(FormatNumber(""));
Console.WriteLine(FormatNumber("1"));
Console.WriteLine(FormatNumber("10"));
Console.WriteLine(FormatNumber("100"));
Console.WriteLine(FormatNumber("1000"));
Console.WriteLine(FormatNumber("10000"));
Console.WriteLine(FormatNumber("100000"));
Console.WriteLine(FormatNumber("1000000"));
Console.WriteLine(FormatNumber("10000000"));

Console.WriteLine(FormatNumber(""));
Console.WriteLine(FormatNumber("1"));
Console.WriteLine(FormatNumber("10"));
Console.WriteLine(FormatNumber("100"));
Console.WriteLine(FormatNumber("1,000"));
Console.WriteLine(FormatNumber("10,000"));
Console.WriteLine(FormatNumber("100,000"));
Console.WriteLine(FormatNumber("1,000,000"));
Console.WriteLine(FormatNumber("10,000,000"));
}

private static string FormatNumber(string number) {
return FormatNumber(number, 3);
}

private static string FormatNumber(string number, int groupLength) {
if ( number == null || number.Length == 0 ) { return ""; }

number = number.Replace(",", ""); // Get rid of old formatting
int commas = (number.Length-1)/groupLength;
int lead = (number.Length-1)%groupLength+1;

string formatted = number.Substring(0, lead);
for(int i = 0; i < commas; i++) {
formatted += "," + number.Substring(i*groupLength + lead,
groupLength);
}

return formatted;
}
}

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"John Sutor" <jo********@cinfin.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I need some code that, on each keyup event, will take all of the numbers
typed into the text box and format as they type to look like this
100
1,000
10,000
100,000
1,000,000

John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Following is a code snippet. Obviously you'll need to set MaximumLength
property of TextBox to prevent OverflowException.
private void textBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
FormatText(this.textBox1.Text);
}
private void FormatText(string number)
{
try
{
long i =
Int64.Parse(number,System.Globalization.NumberStyl es.AllowThousands);
this.textBox1.Text = i.ToString("#,0");
this.textBox1.SelectionStart = this.textBox1.Text.Length;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Justin,
The problem is not formatitng the number, but formatting it on a key
event (press / down / up). The user wants to be able to start typing
and the numbers start formatting correctly. Your method, which is very
good and I'll use it,will not work as an event handler.

John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
It will work if called from an event handler.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"John Sutor" <jo********@cinfin.com> wrote in message
news:ub**************@TK2MSFTNGP10.phx.gbl...
Justin,
The problem is not formatitng the number, but formatting it on a key
event (press / down / up). The user wants to be able to start typing
and the numbers start formatting correctly. Your method, which is very
good and I'll use it,will not work as an event handler.

John S

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Saix News | last post: by
4 posts views Thread by Tommi Mäkitalo | last post: by
7 posts views Thread by ilona | last post: by
3 posts views Thread by sparks | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.