Need some Visual Basic help.

  • Thread starter Thread starter SSVegetto
  • Start date Start date
Status
Not open for further replies.
S

SSVegetto

Guest
I got program that calculates 10 percent of a number.

Ok when people enter the number into the textbox and click calculate I want to show the ten percent of a number and that works fine. But when people enter a letter(strings) into the textbox and hit calculate I want to display "error please insert a number instead"

I think I need a if statement but I dont know how it should look, also I want the error to show in the same textbox you enter the numbers in.

I guess this in the calculate button isn't going to work.

If txtSales = string then
            txtSales = "error enter numbers"

        End If

So I would appreciate if someone can help me, because I notice you guys use visual basic on some of your programs.
 
It's already a fews years ago that I last coded in vb, but I think I can help you here.

There is a method for the textbox that is called "IsNumeric" (or something like this).
When the user hits calculate you should do this:

Code: [Select]
Code:
Private Sub Calculateif textbox.IsNumeric = false then  textbox.text = "error please insert a number instead"  exit subend if' And here comes your normal calculating code...End Sub

I'm not sure if the syntax is 100% correct, but that's the if block you need.

 - Alhexx

 - edit -
Ant, btw, I think this topic belongs to tech-related.
 
isn't there in vb possibility to set variables as numbers? such as var in tp or somethin' ;]
 
I appreciate the reply but there is a problem


if textbox.IsNumeric = false then
textbox.text = "error please insert a number instead"
  exit sub
end if


the part that is underlined gives an error saying 'IsNumeric' is not a member of 'System.Windows.Forms.Textbox'

Anyway I think you are getting close I mean that sounds right.
 
I don't know of any IsNumeric method on the TextBox class, but here is a method you could use to test any String value if it is a number or not:

Code: [Select]
Code:
    Public Function IsNumeric(ByVal str As String) As Boolean        Try            Integer.Parse(str)        Catch ex As Exception            Return False        End Try        Return True    End Function
 
SSVegetto: Which version of VB are you using? VB.net?

I know there was a function called "IsNumeric" or "IsNumericValue" or something like this for textboxes in VB 6...

However, as I said before, it's years since I have written VB programs...

 - Alhexx
 
SSVegetto: Which version of VB are you using? VB.net?

I know there was a function called "IsNumeric" or "IsNumericValue" or something like this for textboxes in VB 6...

However, as I said before, it's years since I have written VB programs...

 - Alhexx

Yeah it is IsNumeric, the instructor told us in class about it.
Something like this but you were almost right.


If IsNumeric(txtSales.Text) Then
            Sales = txtSales.Text
            'If it is not it will bring up an error message
        Else
            MessageBox.Show("Error Message here")
            'this brings the focus in textsales box
            txtSales.Focus()
            'this selects the string letters.
            txtSales.SelectAll()
 
it is simple, put a textbox and a command button, place this code under the command button

If IsNumeric(Text1.Text) = True Then
    MsgBox Text1.Text * 0.1
Else
    MsgBox "Error enter numbers"
End If
 
Status
Not open for further replies.
Back
Top