By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Formatting numbers with events

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.