Wednesday, June 15, 2011

Check all textboxes in form if they contain numbers only(vb.net)

Public Function Chk_If_Numeric(ByRef frm As Form) As Boolean
'the function check from the textboxes in a certain form'
Dim cControl As Control

Chk_If_Numeric = True  'set the variable to default value'
    For Each cControl In frm 'looping trough the form controls'
        If TypeOf cControl Is TextBox Then 'finding only textboxes'
            If Not IsNumeric(cControl.Text) Then 'check if contain only numbers'
                Chk_If_Numeric = False 'if one of them is not we return false'
            End If
        End If
    Next
End Function

Private Sub Command1_Click()
'a test button click '
    If Chk_If_Numeric(Form1) Then
        MsgBox "all numbers"
    Else
        MsgBox "not all are numeric"
    End If
End Sub

No comments:

Post a Comment