• We just launched and are currently in beta. Join us as we build and grow the community.

Disable Paste in A Numeric TextBox in VB.NET

thebluesfc

Meme Archivist
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
40
Likes
183
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
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. Dim

    Clr As

    Color =

    TxtBox.

    BackColor

  3. TxtBox.

    ReadOnly

    =

    LockTextBox
  4. TxtBox.

    BackColor

    =

    Clr

  5. 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. 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. If

    Char

    .

    IsDigit

    (

    num)

    Or

    num =

    ChrW

    (

    Keys.

    Back

    )

    Then

  4. ReadOnlyText(

    TextBoxNumeric, False

    )
  5. e.

    Handled

    =

    False

  6. Else

  7. If

    TextBoxNumeric.

    Text

    .

    Contains

    (

    "."

    )

    =

    True

    Then
  8. e.

    Handled

    =

    True
  9. Else

  10. If

    num =

    Char

    .

    Parse

    (

    "."

    )

    Then
  11. ReadOnlyText(

    TextBoxNumeric, False

    )
  12. e.

    Handled

    =

    False
  13. Else
  14. e.

    Handled

    =

    True
  15. End

    If
  16. End

    If

  17. End

    If

  18. End

    Sub

Output:

disablepaste.png


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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top