473,406 Members | 2,707 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,406 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 2775
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 print. I have a __str__ method which outputs...
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 boxes. they display without a problem but i would...
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. Any ideas? -- Tommi Mäkitalo
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 my report contains two elements: And let's...
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 combination. Or something different in the future. At...
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 numeric, Long Integer, and formatted with 5 zero's . ...
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 from db if the number is Canadian, US, or some...
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 with the fields the conditional formatting works...
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 numbers from the people. some are -.95 and later they...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.