Search code examples
vb.netwinformsvisual-studio-2003

How to validate that at least one textbox was filled in (VB.Net)


I have 4 textboxes, and I am trying to validate that at least one of the textboxes is filled before they hit search to check their search criteria.

My Problem: My code is working, but when I want to continue with one filled field, it still shows the messageBox.

If txtMember.Text = "" Then
            MessageBox.Show("Please enter a value!")
        ElseIf txtReference.Text = "" Then
            MessageBox.Show("Please enter a value!")
        ElseIf txtName.Text = "" Then
            MessageBox.Show("Please enter a value!")
        ElseIf txtCode.TextLength = "" Then
            MessageBox.Show("Please enter a value!")
        Else
            SearchClick()
        End If

How do I fix this? Your help would be much appreciated.


Solution

  • Is something like below what you want? So if every text box is blank you want a message box to appear otherwise if one of the text boxes is filled in then do SearchClick How to use If Else. Learning to step through the code, might have helped you realise what was happening in your code.

    If txtMember.Text = "" AndAlso txtReference.Text = "" AndAlso txtName.Text = "" AndAlso  txtCode.TextLength = "" Then
        MessageBox.Show("Please enter a value!")
    Else
        SearchClick()
    End If