TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Richard Vannoy
NA
11
4.8k
Input Validate: How to accept only one or two digit integer?
Aug 12 2015 6:54 PM
I'm programming a game. I want to accept only a one or two digit number.
Only digits 0-9 allowed.
Only 1 or 2 digits allowed.
When second digit entered, number is accepted. Program goes to range check (Message to user if number is odd or greater than 20)
For one digit number, user has to indicate done by pressing return.
Here is what I have:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (ch == 46 && textBox1.Text.IndexOf('.') != -1)
{
e.Handled = true;
return;
}
if (!char.IsDigit(ch) && ch != 8 && ch != 46)
// 8 = Backspace, 46 = decimal point
{
e.Handled = true; // Exits without taking any further action
}
}
Reply
Answers (
3
)
Using ExifReader to find tags property in Jpeg Image
using System; using System.Collections.Generic; using System