472,353 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 2709
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Sommers | last post by:
Hi, I have a class whose objects represent physical quantities including uncertainties and units, and I would like more control over the way they...
1
by: Saix News | last post by:
hi, i have a dataset and a number of controls that are bound to it. some of the records being displayed are numerical and are bound to text...
4
by: Tommi Mäkitalo | last post by:
Hi I need to format floating-point-numbers with exact 2 digits after decimal point. I could use printf with "%.2f", but it don't use std::locale....
2
by: Eddy Bee | last post by:
Hi there, I'm encountering an inexplicable problem with page formatting in reports. Here's the easiest way to explain it: The Detail section of...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated...
3
by: sparks | last post by:
We have one database that they are constantly reformatting their inputs. I asked about the changes and they can never get the same types of...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.