By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Share this Question
Share on Google+
2 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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

P: 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.