.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
SP
satyapriyanayak
Subhransu Sekhar Jena
abhays
DotnetUser

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

To Validate Textbox Decimal Values

Posted By:Santosh       Posted Date: October 08, 2009    Points: 10    Category: Windows Form Controls    URL: http://santoshdotnetarena.blogspot.com  
 

for web application validation controls are there to validate control
but in windows application it is little bit difficult
i am giving the below code to validate dacimal values of a textbox

Ex:Let you need to enter a textbox decimal values like(123.34) instead of no rtestriction
no need to set any properties of textbox.

just in KeyPress u call this below method and Pass before and after decimal values.
if u want to enter like(2124.98) so need to pass bd value =4 and ad value = 2



VB.NET
Shared Sub ChkDecimal(ByVal Ctxt As System.Windows.Forms.TextBox, ByVal x As System.Windows.Forms.KeyPressEventArgs, ByVal bd As Int16, ByVal ad As Int16)
Ctxt.Text = Ctxt.Text.Trim
If (Char.IsControl(x.KeyChar) Or Char.IsDigit(x.KeyChar) = True) Or Asc(x.KeyChar) = 46 Or Asc(x.KeyChar) = 45 Or

Asc(x.KeyChar) = 8 Then
If ((Ctxt.Text.IndexOf(".") <> -1 And x.KeyChar = ".") Or (Ctxt.Text.IndexOf("-") = 0 And x.KeyChar = "-") Or

(Ctxt.Text.IndexOf("-") = 0 And Ctxt.SelectionStart = 0 And Asc(x.KeyChar) <> 8)) And Ctxt.SelectionLength = 0 Then
x.Handled = True
ElseIf (Ctxt.Text <> "" And x.KeyChar = "-" And Ctxt.SelectionStart <> 0) Then
x.Handled = True
Else
If Ctxt.Text.IndexOf(".") <> -1 Then
If Ctxt.SelectionStart >= 0 And Asc(x.KeyChar) <> 8 Then
If ((((Mid(Ctxt.Text, Ctxt.Text.IndexOf(".") + 1).Length > ad + 1 And Ctxt.SelectionStart >

Ctxt.Text.IndexOf(".")) Or (Mid(Ctxt.Text, 1, Ctxt.Text.IndexOf(".") + 1).Length > bd And Ctxt.SelectionStart <> "-") And Ctxt.SelectionLength = 0) Or _
((((Mid(Ctxt.Text, Ctxt.Text.IndexOf(".") + 1).Length > ad And Ctxt.SelectionStart >

Ctxt.Text.IndexOf(".")) Or (Mid(Ctxt.Text, 2, Ctxt.Text.IndexOf(".")).Length > bd And Ctxt.SelectionStart <> -1)) And Ctxt.SelectionLength = 0) Then
x.Handled = True
End If
End If
Else
If (Ctxt.Text.IndexOf("-") <> -1 And Ctxt.SelectionLength = 0 And ((Mid(Ctxt.Text, 2).Length > bd - 1 And

x.KeyChar <> "." And Asc(x.KeyChar) <> 8) Or (Ctxt.SelectionStart > bd + 1 And x.KeyChar.ToString.Equals(".")))) Or _
(Ctxt.Text.IndexOf("-") = -1 And Ctxt.SelectionLength = 0 And (((Mid(Ctxt.Text, 1).Length > bd - 1

And x.KeyChar <> "." And Asc(x.KeyChar) <> 8) And Asc(x.KeyChar) <> 45) Or (Ctxt.SelectionStart > bd And

x.KeyChar.ToString.Equals(".")))) Then
x.Handled = True
End If
End If
End If
Else
x.Handled = True
End If
End Sub

C#.NET
public static void ChkDecimal(System.Windows.Forms.TextBox Ctxt, System.Windows.Forms.KeyPressEventArgs x, Int16 bd, Int16

ad)
{
Ctxt.Text = Ctxt.Text.Trim();
if ((char.IsControl(x.KeyChar) || char.IsDigit(x.KeyChar) == true) || System.Convert.ToInt32(x.KeyChar[0]) ==

46 || System.Convert.ToInt32(x.KeyChar[0]) == 45 || System.Convert.ToInt32(x.KeyChar[0]) == 8)
{
if (((Ctxt.Text.IndexOf(".") != -1 && x.KeyChar == ".") | (Ctxt.Text.IndexOf("-") == 0 && x.KeyChar

== "-") | (Ctxt.Text.IndexOf("-") == 0 && Ctxt.SelectionStart == 0 && System.Convert.ToInt32(x.KeyChar[0]) != 8)) &&

Ctxt.SelectionLength == 0)
{
x.Handled = true;
}
else if (Ctxt.Text != "" && x.KeyChar == "-" && Ctxt.SelectionStart != 0)
{
x.Handled = true;
}
else
{
if (Ctxt.Text.IndexOf(".") != -1)
{
if (Ctxt.SelectionStart >= 0 && System.Convert.ToInt32(x.KeyChar[0]) != 8)
{
if (((((Ctxt.Text.Substring(Ctxt.Text.IndexOf(".")).Length > ad + 1 &

Ctxt.SelectionStart > Ctxt.Text.IndexOf(".")) | (Ctxt.Text.Substring(0, Ctxt.Text.IndexOf(".") + 1).Length > bd &

Ctxt.SelectionStart < selectionlength ="="> ad & Ctxt.SelectionStart >

Ctxt.Text.IndexOf(".")) | (Ctxt.Text.Substring(1, Ctxt.Text.IndexOf(".")).Length > bd & Ctxt.SelectionStart < selectionlength ="=" handled =" true;" selectionlength ="="> bd - 1 & x.KeyChar != "." & System.Convert.ToInt32(x.KeyChar[0]) != 8) |

(Ctxt.SelectionStart > bd + 1 & x.KeyChar.ToString().Equals(".")))) | (Ctxt.Text.IndexOf("-") == -1 && Ctxt.SelectionLength

== 0 && (((Ctxt.Text.Substring(0).Length > bd - 1 & x.KeyChar != "." & System.Convert.ToInt32(x.KeyChar[0]) != 8) &

System.Convert.ToInt32(x.KeyChar[0]) != 45) | (Ctxt.SelectionStart > bd & x.KeyChar.ToString().Equals(".")))))
{
x.Handled = true;
}
}
}
}
else
{
x.Handled = true;
}
}

How Was the Coding??
Santosh
http://santoshdotnetarena.blogspot.com


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend