2013-03-11 76 views
0

因此,我的老師希望課程搜索作者姓名的文件,並在文本框(如郵寄標籤)中顯示該作者的所有信息。如何在Visual Basic中搜索文本文件

這是我的代碼,我在下面添加了一張圖片。我真的失去了讓程序接收作者字符串並搜索另一個文件。我可以很好地顯示作者姓名,但是如何搜索該名稱的另一個文件並在該行上顯示關於作者的信息?

Imports System.IO 

Public Class Form1 
' CSCI 6 
' Alex Smutny 
' Lab #3 
' 
' DATE: 3/1/2013 

Dim SR As IO.StreamReader 

' initial start spot for the record Count 
Dim RecordCount As Integer = 0 
Dim Author As String = File.ReadAllText("Authors.txt") 



Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quitBtn.Click 
    ' Properly Exits the application. 

    Me.Close() 
End Sub 

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeBtn.Click 
    readBtn.Text = "Read" 
    SR.Close() 

    ' Changes the button statuses Locked or unlocked and sets default settings. 
    openBtn.Enabled = True 
    quitBtn.Enabled = True 
    readBtn.Enabled = False 
    closeBtn.Enabled = False 
    SearBtn.Enabled = False 

    RecordCount = 0 

    recordNum.Clear() 
    bookNum.Clear() 
    isbnNum.Clear() 
    bookTitle.Clear() 
    authorName.Clear() 
    bookPrice.Clear() 
    bookQuanity.Clear() 
    reorderPoint.Clear() 

End Sub 

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openBtn.Click 
    ' Sets the file to read from and opens it. 
    SR = IO.File.OpenText("Books.Txt") 

    ' Changes the status of the buttons again now that the File is open and ready to read. 

    openBtn.Enabled = False 
    quitBtn.Enabled = False 
    readBtn.Enabled = True 
    closeBtn.Enabled = True 
    SearBtn.Enabled = True 

End Sub 

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles readBtn.Click 
    'Changes the Read button to say next because it makes more since. 
    readBtn.Text = "Next" 
    ' Setting all the variables. 
    Dim srIndex1 As Integer = 18 
    Dim srTitle As Integer 
    Dim srAuthor As Integer 
    Dim srPrice As Integer 
    Dim srDPrice As Double 
    Dim srData As String 
    Dim srLength As Integer 


    If SR.Peek() = -1 Then 
     MessageBox.Show("End of file") 
    Else 
     ' Starts to read 
     srData = SR.ReadLine 

     ' Increment counter by 1. 
     RecordCount = RecordCount + 1 

     ' Determine the record length. 
     srLength = srData.Length 

     ' gets the book number 
     bookNum.Text = srData.Substring(0, 3) 

     ' ISBN number 
     isbnNum.Text = srData.Substring(4, 13) 

     ' Book title 
     For srTitle = 1 To srLength Step 1 
      If srData.Substring(srIndex1 + srTitle, 1) = "," Then 
       bookTitle.Text = srData.Substring(srIndex1, srTitle) 
       srIndex1 = srIndex1 + srTitle + 1 
       srTitle = srLength + 1 
      End If 
     Next 

     ' Book Author 
     For srAuthor = 1 To srLength Step 1 
      If srData.Substring(srIndex1 + srAuthor, 1) = "," Then 
       authorName.Text = srData.Substring(srIndex1, srAuthor) 
       srIndex1 = srIndex1 + srAuthor + 1 
       srAuthor = srLength + 1 
       Author = authorName.Text 
      End If 
     Next 

     ' Book Price 
     For srPrice = 1 To srLength Step 1 
      If srData.Substring(srIndex1 + srPrice, 1) = "," Then 
       srDPrice = CDbl(srData.Substring(srIndex1, srPrice)) 
       bookPrice.Text = srDPrice.ToString("C") 
       srIndex1 = srIndex1 + srPrice + 1 
       srPrice = srLength + 1 
      End If 
     Next 

     ' Quanity 
     bookQuanity.Text = srData.Substring(srIndex1, 2) 
     srIndex1 = srIndex1 + 3 

     ' Reorder Point 
     reorderPoint.Text = srData.Substring(srIndex1, 2) 

    End If 

    ' record count 
    recordNum.Text = RecordCount 

End Sub 



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    openBtn.Enabled = True 
    quitBtn.Enabled = True 
    readBtn.Enabled = False 
    closeBtn.Enabled = False 
    SearBtn.Enabled = False 


End Sub 

Private Sub SearBtn_Click(sender As System.Object, e As System.EventArgs) Handles SearBtn.Click 
    SR = IO.File.OpenText("Authors.Txt") 

    SR.Close() 
    readBtn.Text = "Restart" 
    RecordCount = 0 
    SR = IO.File.OpenText("Books.Txt") 
End Sub 

末級

Program

作者文件http://pastebin.com/t7C8ye9e 書籍文件http://pastebin.com/y6DNyUFd

所以,我的首要目標就是獲取當前作者的信息,然後從其他文件中得到它並取出所有尾隨空格並將其顯示爲郵件標籤。

回答

0

有很多好方法可以做到這一點。現在讓我們假設每行有一個作者。如果有,您可以使用My.Computer.FileSystem.ReadAllText()獲取整個字符串,並使用String.Split(Environment.NewLine)在每一個新行上分割它。無論如何,你需要有一些方法來分隔列表中的每個作者。 String.Split返回一個String對象的數組。然後,您可以遍歷數組來確定一個術語是否與搜索匹配。這是一個for循環的例子。

'... search term given in param 

'... after loading text file 
Dim StrArr() As String 

StrArr = AuthorsString.Split(Environment.NewLine) 
Dim i as Integer 

For i=0 to StrArr.Length - 1 Step 1 

If StrArr(i).toLower = SearchTerm.toLower Then 

'String is a match! Case insensitivity should make it a little easier for  the user. 

End If 

Next 

讓我知道如果您有任何問題。