LabRat
LabRat

Reputation: 2014

multiple textboxes one If statement.. Vb.net

Hi I want to know if I can check textbox1 textbox2 and textbox3 for null or empty string as one? I have the following exmple I tryed myself but i get an error

    If String.IsNullOrEmpty(TextBox1.Text) Or (TextBox2.Text) Or (TextBox3.Text) Then
        'somthing
    Else
        'somthing else
    End If

Upvotes: 0

Views: 2093

Answers (3)

Moumit
Moumit

Reputation: 9510

That's why the concept of Function, Params is there ...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If CheckAllTextBoxsAreEmpty(TextBox1, TextBox2, TextBox3) Then
        'somthing
    ElseIf CheckAllTextBoxsAreEmpty(TextBox1, TextBox2) Then
        'somthing else
    Else
        'somthing else
    End If
End Sub

Public Function CheckAllTextBoxsAreEmpty(ParamArray txtBoxs() As TextBox) As Boolean
    For Each txtBox As TextBox In txtBoxs
        If Not String.IsNullOrEmpty(txtBox.Text) Then
            Return False
        End If
    Next
    Return True
End Function

Upvotes: 0

Fabio
Fabio

Reputation: 32445

You need execute String.IsNullOrEmpty on every Textbox.Text.

Use logical operator OrElse.
If first boolean expression return True and OrElse used, then other expressions will not be executed, while Or operator executes always all sides(expressions).

If String.IsNullOrEmpty(TextBox1.Text) OrElse 
   String.IsNullOrEmpty(TextBox2.Text) OrElse 
   String.IsNullOrEmpty(TextBox3.Text) Then
    'something
Else
    'something else
EndIf

Upvotes: 2

Zohar Peled
Zohar Peled

Reputation: 82474

If String.IsNullOrEmpty(TextBox1.Text) Or String.IsNullOrEmpty(TextBox2.Text) Or String.IsNullOrEmpty(TextBox3.Text) Then
'somthing
Else
'somthing else
EndIf

Upvotes: 0

Related Questions