Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am working on a program and it calls for input validation for the text that the user enters into the text box. It does not want negative numbers or strings (i.e. "abcd...").

 

I understand how to do the negative numbers part of the validation but not the string part of the validation.

 

Here is what I have so far. I thought the IsNumeric funtion would work on it but I have a run-time error in and when I enter a letter (such as ABC) it doesnt do anything.

 

 

Dim distance As Long



       'Gets the distance for medium
     If IsNumeric(txtDistance.Text) Then
       distance = Val(txtDistance.Text)
       'Validate text to make sure no negatives number or strings where 
       'entered into the text box
       If distance < 0 Then
           MessageBox.Show("Please enter positive number for " & _
           "the distance for your selected medium.", "Text Error", MessageBoxButtons.OK, _
           MessageBoxIcon.Error)
        End If
     End If

 

 

Also Does anyone know how to round numbers to just two decimal places. I am using the data entered in the textbox with some variables and then displaying the answer in a label, but when the math is complete I get 9.099990999 as an output and I just want it to be 9.09

 

Thanks ahead of time for the help!

Posted
If you're writing a Windows Forms app, you can use the NumericUpDown control instead of the Textbox control. That way, you don't have to code much (if at all) to validate the numeric input because it's automatically done for you. Look for the NumericUpDown control in the Toolbox.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...