Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2007, 05:32 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default SOLUTION - Creating an 'integer' only textbox

This was something that frustrated me for about an hour today and thought that I would share my solution.

Lanaguage: C#
Proficiency: Beginner (Understanding of events)
IDE: VS 2003
Scenario: On a Windows application you have a textbox in which you want the user to only insert digits (0-9) and also allow them to erase what they have put in the textbox (the backspace key)

Solution:
In your code behind you will need to WireUp the textbox's KeyPress event so:
Code:
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtNumericKeyCheck);
Now create your method to handle the event:
Code:
private void txtNumericKeyCheck(object sender, KeyPressEventArgs e)
{
}
Pretty cut and dry thus far so lets add the code to determine which key the user is pressing:
Code:
private void txtNumericKeyCheck(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
int i = (int)c;
}
Again this is very basic, our event args pass in the key that was pressed. The second part is personal prefereance, however, IMHO conditionals are easier to write when your dealing with numbers so we convert the Character to its corrosponding ASCII number.

Lastly we need to check if the keystroke was numeric, the backspace, or anything else:

Code:
private void txtNumericKeyCheck(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
int i = (int)c;
    if((i < 48 || i > 57) && i != 8)
    {
    e.Handled = true;
    return;
    }
}
The keystroke 0 (Zero) is 48 and 9 is 57 so that becomes the first half of our condition and secondly we check for the value of 8 (backspace); if the code passes those checks the keystroke is registered inside of the textbox if the checks fail we call:

Code:
e.Handled = true;
which handles the keystroke and it is not registered inside of the textbox!

Also, if you are watching the length of textbox, want to verifiy the data, etc, handle it in the textboxes textChanged event as it will fire AFTER the keypress event.

Lastly this isn't, by far, the only way to achieve the end result. You could, for example, add a reference to Microsoft.VisualBasic and use the IsNumeric() function that is contained within it, place it inside a method that handles your textChanged event and if the function returns false, you know that non-numeric data is present. (I have had so so results with IsNumeric() in that data i thought would return false returned true and vice versa)

All and all pretty simple.




================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old January 24th, 2007, 03:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The .NET 2.0 framework has a control "MaskedTextBox" which allows the assignment of an input mask.

http://msdn2.microsoft.com/en-us/lib...edtextbox.aspx

-Peter
 
Old January 24th, 2007, 03:58 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

If only the powers that be would purchase me VS 2005 =[

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old January 25th, 2007, 12:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You don't technically need any development environment to program in .NET. While it's significantly more difficult, it's possible.

-Peter
 
Old January 25th, 2007, 12:10 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Oh yes, I am well aware. Though after using VS I will never code .Net in notepad ever again ><

================================================== =========
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
 
Old January 25th, 2007, 01:31 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Thanks for sharing your code. Two points:
  • You could use Char.IsDigit to avoid having to know the codes for digits
  • There's a big business case for VS2005 and .NET 2. Even if the VS Express editions are insufficient for your needs a one off purchase of MSDN gives you a slew of software and a never ending licence for development and testing use including an Office licence, well worth it in my opinion

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer I/O in assembly kaycg BOOK: Professional Assembly Language 2 May 11th, 2007 05:00 AM
text vs integer paul20091968 Access VBA 2 February 13th, 2007 10:50 PM
Integer Size Miquella C++ Programming 7 November 6th, 2005 10:31 PM
Integer Check awais_syed VB.NET 2002/2003 Basics 2 December 18th, 2004 02:04 AM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.