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