问题描述
当按下保存按钮时,表单允许我将客户详细信息保存在文本文件中,每次用户按下保存时,我还会对文本框中输入的电话号码进行状态检查和长度验证。问题是,当验证失败时,不应将客户详细信息保存到文本文件中,目前正在保存详细信息。
我尝试过的:
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")
推荐答案
j snooze
我想,这是你可能想要的?我不熟悉Forms和VB.NET中的所有爵士乐,明天我们在爱尔兰也有St Patrick庆祝活动,所以我快速写了这段代码。 txtBoxes字符串数组模拟真实TextBoxes的内容。这有效,但它只是一个原型,所以你可以改变并适应你的应用程序。如果你有更多的问题不要犹豫,只要问...是的,还需要更多验证正确的电话号码(IsNumeric等)祝你好运。
ATeDe
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
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
这篇关于如果使用VB验证失败,如何在文本文件中保存详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!