Saturday, July 4, 2009

Numeric TextBox with One . and - Sign in .NET Windows Form

The following VB.NET function NumericValidation is used to prevent the users from entering characters other than digits in text box

* Drag the TextBox Control and drop on Form and name it as TextBox1

* Call the function NumericValidation Whenever an key is pressed (KeyPress Event of Textbox)

/* This function accepts a single character entered in the text box as Input */

/* This function Permits Digits [0-9] */

/* This function permits a Single Dot for decimal value */

/* This function permits 4 digits after decimal point */

/* This function Permits a single - sign for Negative Symbol */

/* This function restricts Negative sign to be at the First Character */

/* This function does not permit 0 as first character */


VB.NET CODE


[CODE]

Private Function NumericValidation(ByVal chr As Char)


Dim index As Integer

Dim str As String = TextBox1.Text.Trim


'First Digit should not be 0

If chr = "0" And str = "" Then

Return False
End If

'Check for duplicate decimal digit
If (chr = ".") Then

index = str.IndexOf(".")
If (index > 0) Then

'Already dot is there
Return False
End If


End If

'Restrict 4 digit numbers after decimal point

index = str.IndexOf(".")

If (index > 0) Then

If (str.Length - index) > 4 Then

'Not More than 4 decimal digits after dot
If Not Char.IsControl(chr) Then 'To allow back space
Return False
End If

End If
End If


If Char.IsDigit(chr) Then 'If it is a digit allow

Return True
End If

If (chr = "-") Then '- (Negative) sign should come first

If (str.Length > 0) Then

Return False
End If

End If


If (Not Char.IsDigit(chr)) Then

' Non Digits are not allowed

If (Not (chr = ".") And Not (chr = "\b") And Not (chr = "-") And Not Char.IsControl(chr)) Then 'Other than back space or dot or negative sign
Return False

End If

End If

Return True
End Function


//Whenever an Key is Pressed We just need to call NumericValidation function with the Key pressed

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

'If the key entered does not meet the criteria

If Not NumericValidation(e.KeyChar) Then

e.Handled = True //Invalid value
Beep() //Beep sound

End If


End Sub

[/CODE]

Valid and Invalid Data

Valid Examples

-1.7897
23.7977

InValid Examples - Will not be permitted

A1235
0788
23.7.2
7-89

C# CODE

Add the Handler textBox1_KeyPress in the Entry Point

Whenever an Key is Pressed we have to Call NumericValidation with the pressed Character.




public Form1()
{
InitializeComponent();

//Add the handler for KeyPress Event for textbox

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
}


//Whenever an Key is Pressed We just need to call NumericValidation function with the Key pressed


private void textBox1_KeyPress(Object sender, System.Windows.Forms.KeyPressEventArgs e)
{

if (!(NumericValidation(e.KeyChar))) 'If not valid do not allow
{

e.Handled = true;

}
}

//Numeric Validation Function
private bool NumericValidation(char chr)
{

string str = textBox1.Text.Trim();

int index = 0;


//First Digit should not be 0

if (chr == '0' & string.IsNullOrEmpty(str))
{
return false;
}

//Check for duplicate decimal digit
if ((chr == '.'))
{

index = str.IndexOf(".");
if ((index > 0))
{

//Already dot is there
return false;

}
}

//Restrict 4 digit numbers after decimal point
index = str.IndexOf(".");
if ((index > 0))
{
//Already dot is there

if ((str.Length - index) > 4)
{

//More than 4 decimal digits after dot
if (!char.IsControl(chr))
{
//To allow back space
return false;

}
}
}

if (char.IsDigit(chr))
{
return true;
}

if ((chr == '-'))
{
//- sign should come first

if ((str.Length > 0))
{
return false;

}
}


if ((!char.IsDigit(chr)))
{

// Non Digits are not allowed

if ((!(chr == '.') & !(chr == '\b') & !(chr == '-') & !char.IsControl(chr)))
{
//Other than back space or dot or negative sign

return false;

}
}

return true;
}



same code snippets available under
http://www.dotnetspider.com/resources/29500-C-Numeric-Text-Box.aspx

No comments:

Post a Comment