Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numerical Value 1

Status
Not open for further replies.

ktchan741

Technical User
Dec 7, 2003
28
SG
I created a field on a form using a textbox.
Can anyone advise how to make textbox accept numeric value (with decimal) only? Now, whenever I keyed-in a number with decimal (e.g. 1.130), only 1 appears, the decimal places disappear?
Advise appreciated.
Thanks.
 
Use KeyPress event of the TextBox control inorder to determine KeyChar of the key user has pressed. If the associated KeyChar is numeric then except the values else not. The following code allows numeric vales and single "."

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'Restirct user to key numeric values
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not e.KeyChar = "." Then
e.Handled = True
End If

'Check for double "."
If e.KeyChar = "." Then
If InStr(TextBox1.Text, ".") = 0 Then
e.Handled = False
Else
e.Handled = True
End If
End If
End Sub


 
It seems as though this quetion is asking about every other day. Try doing a search. For example:

thread796-873129

 
PankajBanga,
Thanks for the advise.
Based on your code, I am able to ensure that user can only key-in a numerical value in the text box.

However, the numerical value I key-in become 0 when it is shown in the Table and the printout. Any other question is there any way to control the number of decimal places an user can enter?

Advise pls.
 
Hey ktchan741,

For limiting the number of characters in the textbox.
I found this site after a quick google:

Code:
Function LimitTextInput(source) As String
    'put the next line in the Textbox_KeyPress event
    'KeyAscii = LimitTextInput(KeyAscii)
    'change Numbers with any other character
    Const Numbers$ = "0123456789."
    'backspace =8
    If source <> 8 Then
    If InStr(Numbers, Chr(source)) = 0 Then
            LimitTextInput = 0
            Exit Function
        End If
        End If
    LimitTextInput = source
End Function

Pay attention to the comments telling you where to place certain lines. It's not exactly what you're looking for but hopefully you can manipulate it to your needs.

And towards the reading as 0.. perhaps you need to cast it to an integer whenever you read the textbox? I believe CType is the way to do it in .net.

(I'm still new with .Net, but I believe that should help :)

Frank
 
Hi everyone!!
I have an extremely dumb question but i dont really know what to do.. :(

I have a database with a table called "rangos", and i have a field called "rango_acum".. where i want to put some data like "1.021".. and i have a windows form.. where after playing around with some variables and doing some calculation... i get the result in a variable "rangoacumulado" which im attaching to a textbox called "txtrangoacumulado" with the property .text...
What i want to do.. its send to the databe the result of the calculation im doing stored at the variable "rangoacumulado" but everytime i try to update the database.. if the result its "0.324" i get a 0 at the DB.. and if the result its "0.562" or "1.223" or "0.654" i get a 1.. i guess its ceiling and flooring the data to the closest number... but i dont want that, i need the exact number at the DB
My variables are defined as Double, at the Db and the vb.net code...

Can anyone give me clue of whats going on please?

Highly appreciated!!
Thanks guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top