by Gehan Fernando (47 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Fri 4th March 2011
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Disable Paste function for numeric text-box
API Declarations
.Drawing
Private Sub TextBoxNumeric_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles TextBoxNumeric.KeyPress
Dim digit As Char = e.KeyChar
If Char.IsDigit(digit) Or digit = ChrW(Keys.Back) Then
ReadOnlyText(TextBoxNumeric, False)
e.Handled = False
Else
If TextBoxNumeric.Text.Contains(".") = True Then
e.Handled = True
Else
If digit = Char.Parse(".") Then
ReadOnlyText(TextBoxNumeric, False)
e.Handled = False
Else
e.Handled = True
End If
End If
End If
End Sub
Private Sub ReadOnlyText(ByVal TxtBox As TextBox, Optional ByVal LockTextBox As Boolean = True)
Dim Clr As Color = TxtBox.BackColor
TxtBox.ReadOnly = LockTextBox
TxtBox.BackColor = Clr
End Sub
Private Sub TextBoxNumeric_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBoxNumeric.MouseDown
ReadOnlyText(TextBoxNumeric)
End Sub
End Class