0

我是 Visual Basic 2010 的新手,我一直在用这个把头撞到墙上。

我有一个接受用户输入并将其保存到以下格式的文本文件的表单:

“客户编号:”00

能源部,约翰

10350 某街

城市,州邮政编码

电话号码

"账户余额:$" 00.00

“最后付款日期:”月/日/年

我有一个文本框,用户根据选中的复选框输入客户编号或姓氏。这样做的目的是通过姓氏或客户编号在文本文件中搜索客户记录。

搜索时,我希望在列表框中显示搜索中使用的姓氏或客户编号的客户。

这是我下面的代码:

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)   Handles btnSearch.Click
    'Local Variables

    Dim ReadCustomerRecords As StreamReader
    Dim strCustomerNumber As String

    ' Validate Search Criteria
    If chkCustomerNumber.Checked = False And chkLastName.Checked = False Then
        MessageBox.Show("Please select either Customer Number OR Last Name" & vbNewLine   & "to narrow your search.", "Attention")

    End If
    If chkCustomerNumber.Checked And chkLastName.Checked Then
        MessageBox.Show("You may only search by Customer Number OR Last Name." & vbNewLine & "Please revise your search.", "Attention")
    End If

    If chkCustomerNumber.Checked Then
        ReadCustomerRecords = File.OpenText(strCustomerRecordsFile)
        strCustomerNumber = ReadCustomerRecords.ReadLine()
        Do Until strCustomerRecordsFile.Contains(txtSearchFile.Text)

            If strCustomerNumber.Contains(txtSearchFile.Text) Then
                lstCustomerSearch.Items.Add(strCustomerNumber)
                lstCustomerSearch.Items.Add(ReadCustomerRecords.ReadLine())
                lstCustomerSearch.Items.Add(ReadCustomerRecords.ReadLine())
                lstCustomerSearch.Items.Add(ReadCustomerRecords.ReadLine())
                lstCustomerSearch.Items.Add(ReadCustomerRecords.ReadLine())
                lstCustomerSearch.Items.Add(ReadCustomerRecords.ReadLine())
                lstCustomerSearch.Items.Add(ReadCustomerRecords.ReadLine())
            Else
                MessageBox.Show("The customer number you entered is not valid." & vbNewLine & "Please try again or search by Last Name.", "Attention")
            End If
            Return
        Loop
        ReadCustomerRecords.Close()
    End If

    If chkLastName.Checked Then
        ReadCustomerRecords = File.OpenText(strCustomerRecordsFile)
    End If
End Sub
4

1 回答 1

0

这里面的条件:

Do Until strCustomerRecordsFile.Contains(txtSearchFile.Text)

可能会导致您的循环无法正确终止,因为这意味着在文件名包含搜索词之前,请执行循环内的代码。由于文件名永远不会更改,因此您的循环可能无法正确终止。

关于搜索文本文件,您可以执行以下操作:

If chkCustomerNumber.Checked Then
    Dim custRecords() as String = File.ReadAllLines(strCustomerRecordsFile)
    Dim isRecordFound as Boolean = false

    For i As Integer = 0 To custRecords.Length - 1
         If custRec(i).Contains(txtSearchFile.Text) Then
             isRecordFound = true;

             lstCustomerSearch.Items.Add(custRec(i))
             lstCustomerSearch.Items.Add(custRec(i + 1))
             lstCustomerSearch.Items.Add(custRec(i + 2))
             lstCustomerSearch.Items.Add(custRec(i + 3))
             lstCustomerSearch.Items.Add(custRec(i + 4))
             lstCustomerSearch.Items.Add(custRec(i + 5))
             lstCustomerSearch.Items.Add(custRec(i + 6))

             Exit For
         End If            
    Next

    If Not isRecordFound Then
         MessageBox.Show("The customer number you entered is not valid." & vbNewLine & "Please try again or search by Last Name.", "Attention")
    End If
End If

所以有点不同,但是:

  1. 我们使用File.ReadAllLines将文本文件中的所有行读入字符串数组(每个数组元素为一行)。您仍然可以StreamReader像现在一样使用,但为简单起见,示例使用File.ReadAllLines
  2. 一旦我们获得了您文件的内容,我们就可以遍历并找到相关的客户 ID(有关此问题的评论,请参见下文)
  3. Once we have found the customer ID we replicate what you were doing with reading the next lines via the StreamReader with looking at the next elements in the array (noting that each array element is a file line)...A defensiveness check for you to implement here would be that we are trying to "look ahead" in the array by a number of elements but if there aren't that many elements left in the array you'll get an exception about the index being out of bounds
  4. We can Exit For from the loop after a record has been found because it's a bit pointless to proceed any further

PS. For your file format an alternative for you to consider is to have an entire record on one line and delimited (e.g. you could use a "|" or something like that to separate records) instead of on multiple lines.

PPS. Be careful with using the Contains clause for searching because "02" would also match an address like "1002 Fake Street" instead of an ID you are looking for so you might dredge up false positives. Tweaking your file format should help you get around this.

于 2013-03-30T23:35:29.200 回答