Author Topic: Need some Visual Basic help.  (Read 3775 times)

SSVegetto

  • *
  • Posts: 49
    • View Profile
Need some Visual Basic help.
« on: 2005-10-13 20:26:16 »
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.

Alhexx

  • *
  • Posts: 1894
    • View Profile
    • http://www.alhexx.com
Need some Visual Basic help.
« Reply #1 on: 2005-10-13 21:30:02 »
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]
Private Sub Calculate

if textbox.IsNumeric = false then
  textbox.text = "error please insert a number instead"
  exit sub
end 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.

hay

  • *
  • Posts: 211
  • If I had a buggy, I'd cross the southern desert
    • View Profile
Need some Visual Basic help.
« Reply #2 on: 2005-10-13 23:05:50 »
isn't there in vb possibility to set variables as numbers? such as var in tp or somethin' ;]

SSVegetto

  • *
  • Posts: 49
    • View Profile
Need some Visual Basic help.
« Reply #3 on: 2005-10-14 01:08:48 »
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.

FF7ExpNeg1

  • *
  • Posts: 16
    • View Profile
Need some Visual Basic help.
« Reply #4 on: 2005-10-14 04:19:19 »
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]

    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

Alhexx

  • *
  • Posts: 1894
    • View Profile
    • http://www.alhexx.com
Need some Visual Basic help.
« Reply #5 on: 2005-10-14 15:36:53 »
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

  • *
  • Posts: 49
    • View Profile
Need some Visual Basic help.
« Reply #6 on: 2005-10-19 14:11:56 »
Quote from: 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()

N-Y-M

  • Guest
Need some Visual Basic help.
« Reply #7 on: 2005-10-19 17:16:51 »
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