Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The form allows me to save customer details in a text file when the button "save" is pressed, I am also carrying a presence check and a length validation of the phone number inputted in a text box every time the user presses save. The problem is that when the validation fails the customer details should not be saved into the text file and currently the details are being saved.

What I have tried:

Dim emptyTextBoxes =
      From txt In Me.Controls.OfType(Of TextBox)()
     Where txt.Text.Length = 0
 Select txt.Name
     If emptyTextBoxes.Any Then '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'

     End If
     MessageBox.Show(String.Format("Please fill following textboxes: {0}",
                     String.Join(",", emptyTextBoxes)))


     If txtBox5.Text.Length < 9 Then
         MsgBox("Phone numbers must be at least 9 digits long") ' if the value entered in text box 5 is < 9 then the respective message box would be shown'
         txtBox5.Focus()
     End If
     If txtBox5.Text.Length > 11 Then
         MsgBox("Phone numbers must be of a maximum of 11 digits long") ' if the value entered in text box 5 is > 11 then the respective message box would be shown'
         txtBox5.Focus()
     End If

     Dim Filenum As Integer = FreeFile()


     FileOpen(Filenum, "Z:\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")
Posted
Updated 17-Mar-18 16:31pm
Comments
j snooze 16-Mar-18 17:40pm    
many ways you can do this. You could set a boolean value inside your validation if logic, set it to false on a validation failure, then put an if condition around your text file save. Right now you have no condition surrounding the text file save so it will naturally save every time no matter what.

Hi
j snooze


I think, this is what you might looking for? I'm not very familiar with Forms and all that jazz in VB.NET, also tomorrow we have St Patrick celebration here in Ireland, so I wrote quickly this piece of code. txtBoxes array of string simulates content of your real TextBoxes. This works but it is only a prototype, so you have change and adapt to your application. If you have more questions don't hesitate, just ask... Yeah, also more validation for correct phone number would be required (IsNumeric and so on) Good luck.

ATeDe




Sub validateTxtBoxes()

        ' An array of type String() as equivalent of your numbers in TXT boxes
        Dim txtBoxes As String() = {"1234567", "123456", "", "123456789", "987654321", "0", "whatever"}

        Dim Result =
            (
                From Tel In txtBoxes.Select(Function(Number, Index) _
                         New With
                           {
                               .Number = Number,
                               .Index = Index,
                               .Length = Number.Length
                           }
                  )
                Order By Tel.Index Ascending
            )

        Dim i, j As Integer
        Dim WrongTxtBoxes As New System.Text.StringBuilder
        Dim Customers As New System.Text.StringBuilder
        For Each box In Result

            ' Presume that correct phone number is 9 digits long, all the rest is WRONG
            If Result(i).Number.Length <> 9 Then
                Console.WriteLine("The Box {0} is WRONG {1}",
                      Result(i).Index,
                      Result(i).Number)
                WrongTxtBoxes.Append(i.ToString & Space(1))
            Else
                Customers.Append("Customer:" & i.ToString & vbTab & "Number:" & vbTab & Result(i).Number & vbCrLf)
                j += 1
            End If
            i += 1
        Next



        If i > 0 Then

            'this piece of code looks for all text boxes in the form, 
            'If any Then Of them are blank their name would be included 
            'In the messagebox which reminds the user To fill them In
            MsgBox(String.Format("Please fill following textboxes: {0}", WrongTxtBoxes.ToString))

        End If

        If j = 0 Then
            MsgBox("NO  customer has been successfully registered!!!")

        Else

            MsgBox(j & " customer has been successfully registered")

            Dim Filenum As Integer = FreeFile()
            FileOpen(Filenum, "Z:\Desktop\Customers.txt", OpenMode.Append)
            'Text file is opened'

            PrintLine(Filenum, Customers.ToString)
            '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'
        End If


    End Sub
 
Share this answer
 
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Button to validate Phone numbers typed into TextBoxes
        Dim emptyTextBoxes =
                            From txt In Me.Controls.OfType(Of TextBox)()
                            Where txt.Text.Length = 0
                            Select txt.Name

        ' An array of type String() as equivalent of your Phones in TXT boxes
        Dim txtBoxes =
                        From txt In Me.Controls.OfType(Of TextBox).Reverse.ToArray
                        Select txt.Text

        Dim Result =
                     From txt In txtBoxes.Select(Function(Phone, Index) _
                         New With
                           {
                               .Phone = Phone,
                               .Index = Index,
                               .Length = Phone.Length
                           })
                     Order By txt.Index Ascending

        Dim i, j As Integer
        Dim WrongTxtBoxesA As New System.Text.StringBuilder
        Dim WrongTxtBoxesB As New System.Text.StringBuilder
        Dim Customers As New System.Text.StringBuilder
        For Each box In Result
            ' Presume that correct phone number IsNumeric and 9 digits long, otherwise is WRONG
            If Not (IsNumeric(Result(i).Phone.ToString)) Or Result(i).Phone.Length <> 9 Then
                WrongTxtBoxesA.Append(String.Format("The Box {0} is WRONG {1} {2}", Result(i).Index, Result(i).Phone, vbCrLf))
                WrongTxtBoxesB.Append(i.ToString & Space(1))
            Else
                Customers.Append("CustomerID:" & i.ToString & vbTab & "Phone:" & vbTab & Result(i).Phone & vbCrLf)
                j += 1
            End If
            i += 1
        Next

        If i > 0 Then
            MsgBox(String.Format("{0} {1} Please update following textboxes: {2}", WrongTxtBoxesA.ToString, vbCrLf, WrongTxtBoxesB.ToString))
        End If

        If j = 0 Then
            MsgBox("NO  customer has been successfully registered!!!")
        Else
            MsgBox(String.Format("{0} {1} {2} customer has been successfully registered ", Customers.ToString, vbCrLf, j))

            Dim Filenum As Integer = FreeFile()
            FileOpen(Filenum, "Z:\Desktop\Customers.txt", OpenMode.Append)
            'Text file is opened'

            PrintLine(Filenum, Customers.ToString)
            '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'
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Button to populate 7 TxtBoxes and 7 labels
        Dim Boxes As String() = {"1234567xx", "123456xxx", "", "123456789", "987654321", "0", "PhoneNumber"}

        Dim txtboxes() As TextBox = Me.Controls.OfType(Of TextBox).Reverse.ToArray
        Dim labels() As Label = Me.Controls.OfType(Of Label).Reverse.ToArray

        Dim i As Integer
        For Each box In txtboxes
            box.Text = Boxes(i)
            i += 1
        Next
        i = 0
        For Each label In labels
            label.Text = "Box" & String.Format(i)
            i += 1
        Next
    End Sub
End Class
 
Share this answer
 
Comments
Maciej Los 18-Mar-18 10:31am    
Why did you post another answer?
ATeDe 18-Mar-18 17:33pm    
Hey Maciej, Instead asking funny questions help, dear man, to improve solution ...Can't you see that is far from perfect?

Module Module1
' emulate the TextBox/ Label array as in the version VB6
Public txtBox() As TextBox = {Form1.TextBox1, Form1.TextBox2, Form1.TextBox3, Form1.TextBox4, Form1.TextBox5, Form1.TextBox6, Form1.TextBox7}
Public myLabel() As Label = {Form1.Label1, Form1.Label2, Form1.Label3, Form1.Label4, Form1.Label5, Form1.Label6, Form1.Label7}
Public Phones As String() = {"1234567xx", "123456xxx", "", "123456789", "987654321", "0", "PhoneNumber"}
End Module

Then....

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim i As Integer
For Each lbl In myLabel
myLabel(i).Text = "Customer" & String.Format(i + 1)
i += 1
Next

End Sub

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