none
Check if textboxes are empty then RRS feed

  • Question

  • Hello,

    With this i check if some textboxes are empty , if not a msgbox.

    But in the example there are 3 but i am using a lot of them.

    Is it possible to show wich textboxes are empty in 1 msgbox and not everytime a msgbox when1 is empty ?

    I am using vb 2010 version

     If String.IsNullOrEmpty(Form1.TextBox1.Text) Then
    
                MsgBox("1 is empty")
    
            End If
            If String.IsNullOrEmpty(Form1.TextBox2.Text) Then
    
    
                MsgBox("2 is empty")
    
            End If
            If String.IsNullOrEmpty(Form1.TextBox3.Text) Then
    
    
                MsgBox("3 is empty")
    
            End If

    i can't use to check if every textbox in a form is empty , because i want to check some of them and not all in the form.

    Wednesday, June 27, 2018 9:13 PM

Answers

  • Thanks Leshay ,

    That works , just what i am looking for.

    Can i combine this with textbox and combobox ?

     With a very small change to the 3rd example I posted in my first post,  it works fine.  As stated in my first post,  you only need to set the Tag property of each control you want checked to "CheckMe".  This can be easily expanded to include other controls as well.

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New System.Text.StringBuilder
    
            Dim ctrl As Control = Me.GetNextControl(Me, True)
            While ctrl IsNot Nothing
                If (TypeOf ctrl Is TextBox OrElse TypeOf ctrl Is ComboBox) AndAlso ctrl.Tag IsNot Nothing AndAlso ctrl.Tag.ToString = "CheckMe" AndAlso String.IsNullOrEmpty(ctrl.Text) Then
                    sb.AppendLine(ctrl.Name & " is empty")
                End If
                ctrl = Me.GetNextControl(ctrl, True)
            End While
    
            If Not String.IsNullOrEmpty(sb.ToString) Then MessageBox.Show(sb.ToString, "Empty...")
        End Sub
    
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, June 28, 2018 2:45 PM
    • Marked as answer by KeesBlunder Friday, June 29, 2018 6:34 PM
    Thursday, June 28, 2018 2:29 PM
  • Thanks ,

    I will take a look at it.

     Ok.  8)

     The advantage of doing it this way is that it eliminates having to add all the controls to a List or Array.  Just set the Tag properties of the desired TextBoxes and ComboBoxes as mentioned and it will find them on your form,  even if they are in different container controls like GroupBoxes or Panels.


    If you say it can`t be done then i`ll try it

    • Marked as answer by KeesBlunder Friday, June 29, 2018 6:35 PM
    Thursday, June 28, 2018 7:47 PM

All replies

  • i can't use to check if every textbox in a form is empty , because i want to check some of them and not all in the form.

    Put the ones that you want to check into a collection, such as a List(Of Textbox).  Then when you want to check them you can use a loop to iterate over the list, checking each one.

    If you need to identify them (eg, "1", "2") then use the Tag property to hold the identifier.


    • Edited by Acamar Wednesday, June 27, 2018 9:38 PM fmt
    Wednesday, June 27, 2018 9:37 PM
  • Rather than creating a separate list, you can simply loop through Me.Controls.OfType(Of TextBox).

    -EDIT-

    Sorry, Controls.OfType may not exist in 2010... in that case loop though all the controls and test for the type TextBox.

    -/EDIT-

    Also be sure you aren't using default form instances... the use of Form1.TextBoxX in your code looks suspicious.  If that code is in Form1 then it should be Me.TextBoxX.  If that code is in a different form than Form1 then you should have a variable which points to the existing instance of Form1.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Wednesday, June 27, 2018 9:45 PM
    Moderator
  • Hi

    Here is some code that may be what you want

    ' Form1 with any number of
    ' TextBoxes and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      ' to hold the TB you want to check
      Dim tbs As New List(Of TextBox)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add those you wantto check
        tbs.AddRange({TextBox1, TextBox3, TextBox5})
      End Sub
      Function CheckTBempty(tbs As List(Of TextBox)) As List(Of String)
        Dim lst As New List(Of String)
        For Each tb As TextBox In tbs
          If tb.Text.Length < 1 Then
            lst.Add(tb.Name)
          End If
        Next
        Return lst
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim empty As List(Of String) = CheckTBempty(tbs)
        If empty.Count > 0 Then
          Dim s As String = "These are Empty:" & vbCrLf
          For Each tb As String In empty
            s &= tb & vbCrLf
          Next
          MessageBox.Show(s)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, June 27, 2018 9:46 PM
  •  There are several ways you can do this.  Just as another example or two...

    Public Class Form1
        Private TxtBxs As New List(Of TextBox)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TxtBxs.AddRange({TextBox1, TextBox3, TextBox4, TextBox6}) ' only add the textboxes to this list that you want to check for being empty
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim empty() As String = (From tb As TextBox In TxtBxs Where String.IsNullOrEmpty(tb.Text) Select tb.Name & " is empty.").ToArray
            If empty.Length > 0 Then MessageBox.Show(String.Join(Environment.NewLine, empty), "Empty TextBoxes...")
        End Sub
    End Class
     

     
    Public Class Form1
        Private TxtBxs As New List(Of TextBox)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TxtBxs.AddRange({TextBox1, TextBox3, TextBox5, TextBox6}) ' only add the textboxes to this list that you want to check for being empty
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New System.Text.StringBuilder
    
            For Each tb As TextBox In TxtBxs
                If String.IsNullOrEmpty(tb.Text) Then
                    sb.AppendLine(tb.Name & " is empty.")
                End If
            Next
    
            If Not String.IsNullOrEmpty(sb.ToString) Then
                MessageBox.Show(sb.ToString, "Empty TextBoxes......")
            End If
        End Sub
    End Class


     

     Another way could be done by setting the Tag property of each TextBox that needs to be checked to something as Acamar has suggested,  it could be numbers as he showed or even just a simple string identifier which indicates that it is a TextBox that needs to be checked.  For example,  if I have 10 TextBoxes on the Form in the designer window,  I just set the Tag property of each one I want to check,  to "CheckMe" as shown below.

     

     Then you could iterate through all the TextBoxes that are on the form and use the Tag property to identify the ones to check.

    Public Class Form1

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New System.Text.StringBuilder

            Dim ctrl As Control = Me.GetNextControl(Me, True)
            While ctrl IsNot Nothing
                If TypeOf ctrl Is TextBox AndAlso ctrl.Tag IsNot Nothing AndAlso ctrl.Tag.ToString = "CheckMe" AndAlso String.IsNullOrEmpty(ctrl.Text) Then
                    sb.AppendLine(ctrl.Name & " is empty")
                End If
                ctrl = Me.GetNextControl(ctrl, True)
            End While

            MessageBox.Show(sb.ToString, "Empty TextBoxes...")
        End Sub

    End Class
     

     I am sure there are several more variations that you can use though.  8)


    If you say it can`t be done then i`ll try it

    Wednesday, June 27, 2018 10:30 PM
  • I hope these two methods can help you

     For count = 1 To 3
                If String.IsNullOrEmpty(CType((Controls("TextBox" & count.ToString())), TextBox).Text) Then
                    str.AppendLine(("TextBox" & count).ToString)
                End If
            Next
            MessageBox.Show(str.ToString + "is empty")
    For Each cur As Control In Controls
                If TypeOf cur Is TextBox And String.IsNullOrEmpty(cur.Text) Then
                    str.AppendLine(cur.Name.ToString)
                End If
            Next
            MessageBox.Show(str.ToString + "is empty")

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 28, 2018 1:30 AM
  • Thanks Leshay ,

    That works , just what i am looking for.

    Can i combine this with textbox and combobox ?

    Thursday, June 28, 2018 6:16 AM
  • Rather than creating a separate list, you can simply loop through Me.Controls.OfType(Of TextBox).

    That won't easily allow for checking only some, rather than all in the form.

    Thursday, June 28, 2018 9:14 AM
  • Thanks Leshay ,

    That works , just what i am looking for.

    Can i combine this with textbox and combobox ?

    Hi

    You will need to be more specific  - do you want to check if some comboboxes have a zero iterm count?


    Regards Les, Livingston, Scotland

    Thursday, June 28, 2018 12:39 PM
  • Rather than creating a separate list, you can simply loop through Me.Controls.OfType(Of TextBox).

    That won't easily allow for checking only some, rather than all in the form.

    Tag the ones you want to check in the designer properties window, then look for the tag value in code.  Or use a control naming convention that specifies the controls to check.  Or make an inherited type and only look for that custom type.

    You can certainly create the separate list, it just may get messy in the code depending on how many controls you have and is a bit redundant since the Controls collection already has what's needed.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 28, 2018 12:55 PM
    Moderator
  • The second method here is what I would suggest as it will also work for the ComboBoxes and doesn't care how they are laid out in the form.  Just edit it to only care about the existence of the correct tag value (not the control type).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 28, 2018 12:58 PM
    Moderator
  • Thanks Leshay ,

    That works , just what i am looking for.

    Can i combine this with textbox and combobox ?

    Hi

    I would suggest you investigate Reeds suggestions.

    It isn't clear what aspect of a ComboBox you want to check. The only thing I would imagine you would want is the Items count., and furthermore,I can't really see why you would want to do that.

    In any case, here is the code I posted amended to include a check on ComboBox Items count being zero.

    ' Form1 with any number of
    ' TextBoxes and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      ' to hold the CONTROLS to check
      Dim tbs As New List(Of Control)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add those you want to check
        tbs.AddRange({TextBox1, TextBox3, TextBox5, ComboBox2, ComboBox3})
      End Sub
      Function CheckTBempty(tbs As List(Of Control)) As List(Of String)
        Dim lst As New List(Of String)
        For Each tb As Control In tbs
          Select Case tb.GetType
            Case GetType(TextBox)
              If tb.Text.Length < 1 Then
                lst.Add(tb.Name)
              End If
            Case GetType(ComboBox)
              Dim cb As ComboBox = DirectCast(tb, ComboBox)
              If cb.Items.Count < 1 Then
                lst.Add(cb.Name)
              End If
          End Select
        Next
        Return lst
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim empty As List(Of String) = CheckTBempty(tbs)
        If empty.Count > 0 Then
          Dim s As String = "These are Empty:" & vbCrLf
          For Each tb As String In empty
            s &= tb & vbCrLf
          Next
          MessageBox.Show(s)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, June 28, 2018 1:52 PM
  •  Thanks

    I give it a try.

    Why i want to check if there are some textboxes or comboboxes are empty , there is a problem when i am printing one excelsheet.

    Sometimes they empty the boxes and print again , i want to check if the boxes i use are filled in and not blank.

    Thursday, June 28, 2018 2:18 PM
  • Thanks Leshay ,

    That works , just what i am looking for.

    Can i combine this with textbox and combobox ?

     With a very small change to the 3rd example I posted in my first post,  it works fine.  As stated in my first post,  you only need to set the Tag property of each control you want checked to "CheckMe".  This can be easily expanded to include other controls as well.

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New System.Text.StringBuilder
    
            Dim ctrl As Control = Me.GetNextControl(Me, True)
            While ctrl IsNot Nothing
                If (TypeOf ctrl Is TextBox OrElse TypeOf ctrl Is ComboBox) AndAlso ctrl.Tag IsNot Nothing AndAlso ctrl.Tag.ToString = "CheckMe" AndAlso String.IsNullOrEmpty(ctrl.Text) Then
                    sb.AppendLine(ctrl.Name & " is empty")
                End If
                ctrl = Me.GetNextControl(ctrl, True)
            End While
    
            If Not String.IsNullOrEmpty(sb.ToString) Then MessageBox.Show(sb.ToString, "Empty...")
        End Sub
    
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, June 28, 2018 2:45 PM
    • Marked as answer by KeesBlunder Friday, June 29, 2018 6:34 PM
    Thursday, June 28, 2018 2:29 PM
  • Thanks ,

    I will take a look at it.

    Thursday, June 28, 2018 7:35 PM
  • Thanks ,

    I will take a look at it.

     Ok.  8)

     The advantage of doing it this way is that it eliminates having to add all the controls to a List or Array.  Just set the Tag properties of the desired TextBoxes and ComboBoxes as mentioned and it will find them on your form,  even if they are in different container controls like GroupBoxes or Panels.


    If you say it can`t be done then i`ll try it

    • Marked as answer by KeesBlunder Friday, June 29, 2018 6:35 PM
    Thursday, June 28, 2018 7:47 PM
  • Thanks IronRazerz,

    i used it and works great !

    Found something else to change some things.

    When i change the Tag of the textboxes i don't want to check at this moment , i found this.

    It works .

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Form1.TextBox2.Tag = "Uncheck"
            Form1.TextBox3.Tag = "Uncheck"
        End Sub

    Or even better 

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Form1.TextBox2.Tag = "CheckMe"
            Form1.TextBox3.Tag = "CheckMe"
        End Sub

    Thanks !!!!

    Friday, June 29, 2018 6:34 PM