DotNet Academy of Rajesh Rolen

Solutions by Rajesh Rolen

Make textbox numeric only with negative value accept

Lets we want to create a textbox which should take only numeric (double/decimal) value and the value can also be in negative
Solution:
Private Sub txt_KeyPressWithNegative(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtothercharges.KeyPress, txtfreight.KeyPress
Dim decexist As Boolean = False

If (CType(sender, TextBox).Text.IndexOf(".") > 0) And e.KeyChar.Equals("."c) Then
decexist = True
End If
If (e.KeyChar = "-") Then
If (CType(sender, TextBox).Text.StartsWith("-") = True) Then
e.Handled = True
Exit Sub
Else
e.Handled = True
CType(sender, TextBox).Text = "-" & CType(sender, TextBox).Text
CType(sender, TextBox).SelectionStart = CType(sender, TextBox).Text.Length
Exit Sub
End If
End If
If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not (e.KeyChar.Equals("."c))) Or decexist Then
e.Handled = True
End If
End Sub

0 comments:

Post a Comment

About this blog

My Blog List

Advertise On This Site

Site Info

Advertise on this Site

To advertise on this site please mail on RajeshRolen@gmail.com

Information Source

About

Pages

Dot Net Academy

Advertis in This Area of Site

Powered by Blogger.

Followers

Blog Archive

Search This Blog