468,140 Members | 1,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Creating a function tha will allow multiple or different textbox to input only number

i have a code and would like to know how i can change it to suit different or multiple textboxes without typing it again and again.
Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox6_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
  2.  
  3.         Dim tb As TextBox = CType(sender, TextBox)
  4.         Dim chr As Char = e.KeyChar
  5.         Dim iLoc As Integer = 0
  6.  
  7.         If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
  8.             e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
  9.         ElseIf e.KeyChar = "." Then
  10.             If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
  11.                 e.Handled = True
  12.             End If
  13.         ElseIf Not Char.IsControl(e.KeyChar) Then
  14.             e.Handled = True
  15.         End If
  16.  
  17.         iLoc = TextBox6.Text.IndexOf(".")
  18.         If iLoc > 0 Then
  19.             If (TextBox6.Text.Substring(iLoc).Length > 2) And Char.IsControl(e.KeyChar) = False Then
  20.                 e.Handled = True
  21.             End If
  22.         End If
  23.     End Sub
Dec 11 '14 #1
2 1094
Frinavale
9,735 Expert Mod 8TB
You can use that method for this purpose if you want to.

To do that you either have to include Handles TextBoxX.KeyPress to the method or you can use the AddHandler statement to hook up the Keypress event for the TextBoxes to the method.

-Frinny
Dec 11 '14 #2
Thanks a lot, it worked
Dec 15 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.