Check for Textboxes with String value

All I want to do is to check for textboxes with string value if yes then the message box will appear saying (use number).

    For Each t In Me.Controls
        If TextBox1.Text = (String) Then
            MsgBox("Please Use Number")
            Exit Sub
            Exit For
        End If
    Next

Thanks in advance

Upvotes: 0

Views: 6806

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460048

From your error-message i assume that you want to validate that the user entered a numerical value. Then you can either use Int32.TryParse or Double.TryParse or simply enumerate all chars and check if they are digits:

For Each txt In Me.Controls.OfType(Of textBox)()
    Dim allDigit = txt.Text.Trim.Length <> 0 AndAlso _
            txt.Text.All(Function(chr) Char.IsDigit(chr))
    If Not allDigit Then
        MsgBox("Please Use Number")
        Exit Sub
    End If
Next

With Int32.TryParse:

Dim intVal As Int32
Dim isInteger As Boolean = Int32.TryParse(txt.Text, intVal)

(assuming also that you want to validate all TextBoxes on your form)

Upvotes: 1

WozzeC
WozzeC

Reputation: 2660

Here you go:

For Each c As Control In Me.Controls
    If TypeOf (c) Is TextBox Then
        If Not IsNumeric(c.Text) Then
            MessageBox.Show("Not a number")
            Exit Sub
        End If
    End If
Next

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16134

Use regex to validate whether or not textbox contains number.

Eg.

Dim regNumber As New Regex("^\d{1,10}$")
regNumber.IsMatch(TextBox1.Text)

Upvotes: 0

Related Questions