r/visualbasic • • Oct 06 '20

How to check for certain integers in a textbox.

Trying to make a program that will take 10 values from 10 textboxes and add them together.

I've tried "t1.text = Int(1)" But that only works with the number one. How do I make it so it'll check for numbers one to ten?

5 Upvotes

3 comments sorted by

2

u/snang Moderator Oct 06 '20

Reference: IsNumeric()

1

u/RodeoMacon Oct 06 '20 edited Oct 06 '20
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sum = 0.0
        For Each txtbox As TextBox In Controls.OfType(Of TextBox)
            Try
                sum += CDbl(txtbox.Text)
            Catch ex As Exception
                MsgBox("Invalid Entry")
                Exit Sub
            End Try
        Next
        MsgBox(sum.ToString,, "Sum")
    End Sub
End Class

1

u/RodeoMacon Oct 06 '20

I suppose it is rude of me to just plop down code...

The numbers are being treated as Doubles here - unlike Integer in your first attempt

You can iterate through each text box
A try block will catch an error where a non-numeric value is put in
Message boxes are handy and note the double comma in the second one (The msgbox style goes in between, but you don't need one)

You'll have to figure out the logic to only include the numbers from 1 to 10