473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 1273
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

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

Similar topics

2
by: reneeccwest | last post by:
Different user input boxes are automatically populated based on a value of the dropdown box. Can anyone help me on that?
38
by: MLH | last post by:
I have 2 global constants declared in the same global module: Global Const MY_VERSION$ = "1.04" Global Const ProjectName$ = "MyProj" I have 2 global procedures designed to return the value of...
1
by: Andrew | last post by:
Hi, friends, Is there a way to make a TreeView allow multiple selections, like in a List control? Thanks.
2
by: hollykatong | last post by:
hello, is there a way to restricting a textbox input wherein it will only accept two integer with decimal places? for example if i enter 90 or 90.1234 it will allow me and when i try to type 890...
21
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
1
by: Luzuko | last post by:
I would like to know how can i restrict textbox input in VB.net using code instead of VB.net controls. e.g If i want a user to type numbers only in a textbox(ID number textbox), how can i make...
6
by: Adam Clauss | last post by:
We have a service written in C#. The generated code "hardcodes" the service name into the exe. We have a situation where we would like to allow multiple Windows Services to be defined. These...
1
by: Something | last post by:
The idea is you input a number and a recursive function will return a square of the entered number. Example: Enter a number: 5 Square of 5 is 25 The input and display is all figured out,...
4
by: huskar | last post by:
Hi everyone! i am newbie to visual basic.net, and I'm having a headache to do this. Assuming we ask the user to input a number of guests and based on that number of guests..we creating textbox or...
5
Fary4u
by: Fary4u | last post by:
using <input values> text box to another text box in sort of array function to passing multiple value without loading page on one click of button <script type='text/javascript'> function...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.