输入文本框与CSV文件中的数据字符串匹配 [英] Input textbox match with data string in CSV file
问题描述
Private
Sub txtName_LostFocus( ByVal 发件人
作为 对象,
ByVal e 作为系统。 EventArgs )
句柄 txtName.LostFocus
<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
尝试
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
Dim filereader As
新 IO。 StreamReader (" C:\ Users \ hana \ Desktop\SCHOOL 2018(可能)\ VB分配\VB分配\我的项目\学生详细信息.csv" )
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
当 filereader.EndOfStream =
False
详情= filereader.ReadLine.Split("," )
< p style ="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
如果 txtName.Text.ToUpper()= Details(0).ToUpper()
然后
  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
匹配= True
;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
结束 如果
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
结束 虽然
<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
Catch
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
匹配= True
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
MessageBox .Show("学生已添加到系统中",
"" , MessageBoxButtons .OK,
MessageBoxIcon 。警告)
  ;&NBSP;
结束 尝试
<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
匹配= 错误
结束 Sub
我使用这些代码将一串名称与csv文件中的数据进行匹配,当我尝试保存信息时,消息框 " Student
已添加到系统中"。 然而,当我点击其他按钮时,仍会出现消息框,我认为因为我正在使用迷失焦点功能。
关于如何解决这个问题的想法?
消息"学生已被添加到系统中"每当出现错误时都会出现 -
任何错误 - 介于"尝试"之间和"Catch"。因此产生错误的代码出了问题。我的猜测是文件的路径需要双反斜杠("\\")。
试试这个。
Private Sub txtName_LostFocus(ByVal sender As Object,ByVal e As System.EventArgs)处理txtName.LostFocus
尝试
匹配=假;
Dim filereader As New IO.StreamReader(" C:\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ VB Assignment \\ Project Project \\StudentDetails.csv")//字符串中的双反斜杠。
当filereader.EndOfStream = False
Details = filereader.ReadLine.Split(",")
if txtName.Text.ToUpper ()=详情(0).ToUpper()然后
匹配=真
结束如果
结束时
如果Match = True则
MessageBox.Show("学生已添加到系统","",MessageBoxButtons.OK,MessageBoxIcon.Warning)结束如果
Catch
MessageBox.Show("试图找到学生时出错。",""",MessageBoxButtons.OK,MessageBoxIcon.Warning )
结束尝试
结束子
Private Sub txtName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.LostFocus
Try
Dim filereader As New IO.StreamReader("C:\Users\hanna\Desktop\SCHOOL 2018 (MAY)\VB Assignment\VB Assignment\My Project\StudentDetails.csv")
While filereader.EndOfStream = False
Details = filereader.ReadLine.Split(",")
If txtName.Text.ToUpper() = Details(0).ToUpper() Then
Match = True
End If
End While
Catch
Match = True
MessageBox.Show("Student has already been added to the system", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
Match = False
End Sub
I use these codes to match a string of names to the a data in csv file and as I try to save the information, the message box "Student has already been added to the system". However, as I clicked other buttons, the message box still appears which I assume because I am using the Lost focus function. Any idea on how to fix this?
The message "Student has already been added to the system" will appear whenever there is an error - any error - in between the "Try" and the "Catch". So something is wrong with the code that is producing an error. My guess is that the path to the file needs double backslashes ("\\").
Try this.
Private Sub txtName_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.LostFocus Try Match = False; Dim filereader As New IO.StreamReader("C:\\Users\\hanna\\Desktop\\SCHOOL 2018 (MAY)\\VB Assignment\\VB Assignment\\My Project\\StudentDetails.csv") // Double backslashes in the string. While filereader.EndOfStream = False Details = filereader.ReadLine.Split(",") If txtName.Text.ToUpper() = Details(0).ToUpper() Then Match = True End If End While If Match = True Then MessageBox.Show("Student has already been added to the system", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If Catch MessageBox.Show("There was an error trying to find the student.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning) End Try End Sub
这篇关于输入文本框与CSV文件中的数据字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!