473,732 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2795
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.WriteLi ne(FormatNumber (""));
Console.WriteLi ne(FormatNumber ("1"));
Console.WriteLi ne(FormatNumber ("10"));
Console.WriteLi ne(FormatNumber ("100"));
Console.WriteLi ne(FormatNumber ("1000"));
Console.WriteLi ne(FormatNumber ("10000"));
Console.WriteLi ne(FormatNumber ("100000"));
Console.WriteLi ne(FormatNumber ("1000000")) ;
Console.WriteLi ne(FormatNumber ("10000000") );

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

private static string FormatNumber(st ring number) {
return FormatNumber(nu mber, 3);
}

private static string FormatNumber(st ring 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.Substrin g(0, lead);
for(int i = 0; i < commas; i++) {
formatted += "," + number.Substrin g(i*groupLength + lead,
groupLength);
}

return formatted;
}
}

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"John Sutor" <jo********@cin fin.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.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 OverflowExcepti on.
private void textBox1_KeyUp( object sender,
System.Windows. Forms.KeyEventA rgs e)
{
FormatText(this .textBox1.Text) ;
}
private void FormatText(stri ng number)
{
try
{
long i =
Int64.Parse(num ber,System.Glob alization.Numbe rStyles.AllowTh ousands);
this.textBox1.T ext = i.ToString("#,0 ");
this.textBox1.S electionStart = this.textBox1.T ext.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********@cin fin.com> wrote in message
news:ub******** ******@TK2MSFTN GP10.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
1821
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 the quantity and its uncertainty, properly rounded (i.e. the uncertainty to one digit and the quantity to the same precision as the uncertainty), followed by the units (mostly) as they accumulated during whatever calculations led to the quantity's
1
1698
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 like to be able to format the numbers being displayed using thousand separators, etc. could someone please point me in the right direction as to how to do this - i have not been able to find a way of doing this.
4
2700
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
3897
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 say I have the following VB code for the Detail_Format event:
8
3536
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
4
2077
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 . The FK in the personnel table is also Long Integer, and formatted as the original field with zero's. Data Example: Original: 5, 9, 15, 99, 128, etc.
7
398
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 other country. When I retrieve the phone numbers, I need to display them as (###) ###-#### x 99999 if it is a Canadian number or (###) ###-#### Ext. 99999 if it is US phone number. Potentially I'd have other countries added as well which might have...
8
8505
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 perfectly. However, I have code that runs under the ON CLICK event. The code changes the focus to other controls, which means the conditional formatting is no longer displayed. This part makes sense to me. Here's what doesn't make sense. The last...
3
1686
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 might get -.9 while some have been collected in the past as 19.9 later the same data comes to them as 19.99 At first they were wanting us to format the fields so they could not
0
9303
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9233
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9177
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8184
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6732
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4803
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3258
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2714
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.