none
Check if 1 of 3 textboxes is filled RRS feed

  • Question

  • Hello ,

    Simple but don't know how to check if 1 of 3 texboxes is filled and not IsNullOrWhiteSpace.

    They have to fill at leased 1 of the 3 textboxes with text or numbers

    This is not working

      If String.IsNullOrWhiteSpace(TextBox1.Text And TextBox2.Text And TextBox3.Text) Then
                MsgBox("No.")
            Else
                MsgBox("Yes.")
            End If

    Error

    The conversion from string to type Long is invalid.
    Or is this the wrong way to check ?




         
    Tuesday, October 9, 2018 6:20 PM

Answers

  • Check this too:

    If String.IsNullOrWhiteSpace(TextBox1.Text & TextBox2.Text & TextBox3.Text) Then . . .


    • Proposed as answer by Alex Li-MSFT Wednesday, October 10, 2018 1:23 AM
    • Marked as answer by KeesBlunder Wednesday, October 10, 2018 12:27 PM
    Tuesday, October 9, 2018 7:06 PM

All replies

  • Here is one way.

    Public Class Form2
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            If String.IsNullOrWhiteSpace(TextBox1.Text) OrElse String.IsNullOrWhiteSpace(TextBox2.Text) OrElse String.IsNullOrWhiteSpace(TextBox3.Text) Then
                MessageBox.Show("One or more is empty")
            Else
                MessageBox.Show("None empty")
            End If
        End Sub
    
    End Class

    Or

    Public Class Form2
        Private textBoxList As List(Of TextBox)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim result = textBoxList.Any(Function(tb) String.IsNullOrWhiteSpace(tb.Text))
            If result Then
                MessageBox.Show("One or more is empty")
            Else
                MessageBox.Show("None empty")
            End If
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            textBoxList = New List(Of TextBox) From {TextBox1, TextBox2, TextBox3}
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, October 9, 2018 7:01 PM
    Moderator
  • Check this too:

    If String.IsNullOrWhiteSpace(TextBox1.Text & TextBox2.Text & TextBox3.Text) Then . . .


    • Proposed as answer by Alex Li-MSFT Wednesday, October 10, 2018 1:23 AM
    • Marked as answer by KeesBlunder Wednesday, October 10, 2018 12:27 PM
    Tuesday, October 9, 2018 7:06 PM
  • Thanks Viorel_ ,

    Easy to use.

    Wednesday, October 10, 2018 12:27 PM
  • Thanks Karen ,

    I give it a try 

    Wednesday, October 10, 2018 12:28 PM