Description:-
Formatting user input
plays a major role in form data validations, so below example shows how to
validate a text box for taking only numeric values of desired maximum size(example
10digt phone number).
Form1.cs
--------
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
Numerictextbox
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void textBox1_KeyPress(object
sender, KeyPressEventArgs e)
{
if (!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)))
{ e.Handled = true; }
|
//Using Function
}
if
(!(Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)))
{ e.Handled = true; }
}
private
void textBox2_KeyPress(object
sender, KeyPressEventArgs e)
{
int
asciiCode = Convert.ToInt32(e.KeyChar);
if ((asciiCode
>= 48 && asciiCode <=57)||Char.IsControl(e.KeyChar))
|
//Using ANSCII code
{
//Do nothing
}
else
{
e.Handled = true;
}
}
}
}
Design :Form1
Property
|
Event
|
MaxLength=10 (txtbox1)(txtbox2)
|
KeyPress (txtbox1)(txtbox2)
|
Screens:
------
Fig:Form1
Download entire project click below Icon

