Just noticed that you wanted J# not C#. The code will be different, and I don't know J#, but the concept should be similar. Sorry.
Luke
Main Topics
Browse All Topicshow do you allow only a single decimal point to be entered within a textbox.
for example, when entering currency figures requiring a decimal point -- but obviously do not want multiple decimals points appearing.
attached is the javascript code I use for allowing just numbers to be entered within textbox field -- as a starting point.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: LukeyJayPosted on 2008-03-31 at 20:54:40ID: 21251148
Hello.
e.com/Micr osoft/Deve lopment/.N ET/ Visual_ CSharp/Q_2 3275773.ht ml
First of all, if you are unfamiliar with how to use the TextChanged event in C# to trigger an event when someone enters in the text box, see my solution here
http://www.experts-exchang
Now, for the meat of it. Inside the text changed event handler method, you retrieve the contents of the text box like this (assuming its called textBox1)
string charCode = this.textBox1.Text;
once you have it, you can then check the most recent entry to see if it's a number or a decimal point. I can't remember if the least significant digit is in position 0 or in position (length - 1), so in the following code I'll use a 0 for the index, but if it checks the lead digit, just replace the 0 with (charCode.Length-1 I think that's a real option in C#).
if (charCode[0] > 31 && (charCode[0] < 48 || charCode[0] > 57))
{
MessageBox("Enter numerals only or decimal point in this field.");
//Some code to restore the value previously in the textbox (global variable, perhaps?)
}
You'll have to figure out the number for a decimal point in ASCII, I can't remember it now, and add it to the above code. I recommend creating a variable at the beginning of your code somewhere (outside your event handler) called something like this.decimalFlag as a boolean.
private bool decimalFlag = false;
Once you detect a decimal point, you set that flag to true. Every time through the event handler, have an "if" statement that checks to see if the decimal point exists. If it does, only allow one digit to the right of it. If they try to enter another digit, give them a message box saying they can't do that, and replace the text in the text box with what it was before they tried to enter the extra digit. Then put a
return;
and you;re out of the event handler function without any changes to the list box.
When I say replace the value in the text box with what it was before, I mean this: define at the beginning of your program
private string oldText;
Then, at the end of the event handler, put the following line
oldText = this.listBox1.Text;
This gives you something to revert to in case you need it.
If this isn't clear to you, I can get more in depth tomorrow with maybe some personalized code examples.
Luke