Disable Paste in A Numeric TextBox in VB.NET

Today, I will teach you how to create a program that will disable paste in a numeric textbox using vb.net. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Add only a TextBox named TextBoxNumeric in your Form. 3. Now, we will do the coding. We will make first to disable paste in the textbox by creating a sub procedure named ReadOnlyText(ByVal TxtBox As TextBox, Optional ByVal LockTextBox As Boolean = True).
  1.     Private Sub ReadOnlyText(ByVal TxtBox As TextBox, Optional ByVal LockTextBox As Boolean = True)
  2.  
  3.         Dim Clr As Color = TxtBox.BackColor
  4.  
  5.         TxtBox.ReadOnly = LockTextBox
  6.         TxtBox.BackColor = Clr
  7.  
  8.     End Sub
After creating the sub procedure, to totally disable the paste in the textbox, we will code for the MouseDown event of our textbox.
  1.     Private Sub TextBoxNumeric_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBoxNumeric.MouseDown
  2.         ReadOnlyText(TextBoxNumeric)
  3.  
  4.     End Sub
Then, after disabling paste, we will validate the textbox using the keypress event of our textbox.
  1.     Private Sub TextBoxNumeric_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxNumeric.KeyPress
  2.         Dim num As Char = e.KeyChar
  3.  
  4.         If Char.IsDigit(num) Or num = ChrW(Keys.Back) Then
  5.  
  6.             ReadOnlyText(TextBoxNumeric, False)
  7.             e.Handled = False
  8.  
  9.         Else
  10.  
  11.             If TextBoxNumeric.Text.Contains(".") = True Then
  12.                 e.Handled = True
  13.             Else
  14.  
  15.                 If num = Char.Parse(".") Then
  16.                     ReadOnlyText(TextBoxNumeric, False)
  17.                     e.Handled = False
  18.                 Else
  19.                     e.Handled = True
  20.                 End If
  21.             End If
  22.  
  23.         End If
  24.  
  25.     End Sub

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Add new comment