Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a total of 7 text boxes, six of them are named txtBox1,txtBox2,txtBox3,txtBox4,txtBox5,txtBox6, the seventh is named txtBoxSearch,I have tried using the code below to carry out a presence check, the problem is that I only want to carry out the presence check on the first 6 text boxes, the code below includes the txtBoxSearch as well. I don't know how to deal with this, does anyone have any ideas?Thanks.

What I have tried:

      Dim PresenceCheck As Boolean

 Dim emptyTextBoxes =
From txt In Me.Controls.OfType(Of TextBox)()
             Where txt.Text.Length = 0
         Select txt.Name()
        If emptyTextBoxes.Any Then

        End If
        PresenceCheck = False
        'this piece of code looks for all text boxes in the form, if any of them are blank their name would be included in the messagebox which reminds the user to fill them in'


        MessageBox.Show(String.Format("Please fill following textboxes: {0}",
                        String.Join(",", emptyTextBoxes)))
        MessageBox.Show("Customer has not been successfully added")
Posted
Updated 21-Mar-18 6:39am
v2

1 solution

If you would like to get a list of TextBoxes, which name is txtBoxN (where N is a number) and TextBox is not empty, then try this:

VB.NET
Dim pattern As String = "^txtBox\d$"
Dim r As Regex = New Regex(pattern)

Dim notEmptyTextBoxes = Me.Controls.OfType(Of TextBox) _
                            .Where(Function(x) x.Text.Trim.Length > 0 And r.Match(x.Name).Success) _
                            .ToList()

For Each tb As TextBox In notEmptyTextBoxes
    MsgBox(String.Format("{0} {1}", tb.Name, tb.Text))
Next


If you would like to inform a user about textboxes to fill, try this:
VB.NET
Dim msg = String.Join("; ", Me.Controls.OfType(Of TextBox) _
                            .Where(Function(x) x.Text.Trim.Length = 0 And r.Match(x.Name).Success) _
                            .Select(Function(x) x.Name) _
                            .ToList())

MsgBox("Please, fill the following textboxes: " & vbCr & _
        msg, MsgBoxStyle.Information, "Information")
 
Share this answer
 
v2
Comments
faiqaa 21-Mar-18 12:54pm    
okay my fault, let me see whether is works now. Thanks. So basically I'm trying to save customer details and if the text boxes are empty I dont need to save the details
however if they are filled then I will need to.
Maciej Los 21-Mar-18 12:59pm    
BTW: you can edit your comments. Move your mouse over it and on the right side you'll see a pencil. ;)
faiqaa 21-Mar-18 13:18pm    
Hello, can you please have a look at this?, one thing that is happening is that when I've filled in all text boxes and press the button, I receive multiple message boxes showing me the information inputted in various text boxes and the last message box says" Please fill the following text boxes:"), this is happening even when they are filled in.
 Dim PresenceCheck As Boolean
        Dim pattern As String = "^txtBox\d$"
        Dim r As Regex = New Regex(pattern)

        Dim notEmptyTextBoxes = Me.Controls.OfType(Of TextBox) _
                                    .Where(Function(x) x.Text.Trim.Length > 0 And r.Match(x.Name).Success) _
                                    .ToList()

        For Each tb As TextBox In notEmptyTextBoxes
            MsgBox(String.Format("{0} {1}", tb.Name, tb.Text))
        Next
        Dim msg = String.Join("; ", Me.Controls.OfType(Of TextBox) _
                               .Where(Function(x) x.Text.Trim.Length = 0 And r.Match(x.Name).Success) _
                               .Select(Function(x) x.Name) _
                               .ToList())
        PresenceCheck = False
        MsgBox("Please, fill the following textboxes: " & vbCr & _
                msg, MsgBoxStyle.Information, "Information")

        If PresenceCheck = True Then
            Dim Filenum As Integer = FreeFile()
            FileOpen(Filenum, "C:\Users\Windows 7 User\Desktop\Customers.txt", OpenMode.Append) 'Text file is opened'

            PrintLine(Filenum, txtBox1.Text & "," & txtBox2.Text & "," & txtBox3.Text & "," & txtBox4.Text & "," & txtBox5.Text & "," & txtBox6.Text) 'The data entered in the above text boxes is combined together separated with commas and stored into the open text file'

            FileClose(Filenum) 'Once this is done the text file is closed and message below is displayed'

            MessageBox.Show("Customer has been successfully registered")
        End If
Maciej Los 21-Mar-18 13:38pm    
Think about that!
        Dim pattern As String = "^txtBox\d$"
        Dim r As Regex = New Regex(pattern)
        Dim msg As String = String.Join("; ", Me.Controls.OfType(Of TextBox) _
                               .Where(Function(x) x.Text.Trim.Length = 0 And r.Match(x.Name).Success) _
                               .Select(Function(x) x.Name) _
                               .ToList())
        If msg<>"" Then MsgBox("Please, fill the following textboxes: " & vbCr & _
                    msg, MsgBoxStyle.Information, "Information")

        msg = String.Join(",", Me.Controls.OfType(Of TextBox) _
                                    .Where(Function(x) x.Text.Trim.Length > 0 And r.Match(x.Name).Success) _
                                    .Select(Function(x) String.Concat(x.Name, ":", x.Text)) _
                                    .ToList())
        If msg<>"" Then
            Dim Filenum As Integer = FreeFile()
            FileOpen(Filenum, "C:\Users\Windows 7 User\Desktop\Customers.txt", OpenMode.Append) 'Text file is opened'
            PrintLine(Filenum, msg) 'The data entered in the above text boxes is combined together separated with commas and stored into the open text file'
            FileClose(Filenum) 'Once this is done the text file is closed and message below is displayed'
            MessageBox.Show("Customer has been successfully registered")
        End If
faiqaa 21-Mar-18 13:46pm    
now it says "overload resolution failed because no accessible 'Concat' can be called with these arguments".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900