从文本文件中读取信息并显示在列表框中 [英] Read information from text file and display in listbox

查看:24
本文介绍了从文本文件中读取信息并显示在列表框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何让用户在列表框中的另一种表单上写入他们想要查看的信息.基本上,用户会写下他们想要的详细信息(标题、作者、股票、价格、小说或非小说),然后按下记录详细信息,程序会在文本文件中搜索这些值,然后另一种形式会给出一个列表框,他们会在其中看到所有选定的书籍.

I am trying to figure out how to have one form where the user writes in the information they want to see on another form in a listbox. basically the user would write in the details that they want (title, author, stock, price, fiction or nonfiction) and then would press record details and the program would search a text file for those values and then another form would come up with a listbox where they would see all those selected books.

这是我到目前为止的代码,它基本上读取文本文件信息并将其存储到数组中:

Here is the code I have so far and it basically reads the text file information and stores it into an array:

    Dim Title As String
    Dim Author As String
    Dim Stock As Integer
    Dim Price As Double
    Dim Fiction As String
    Dim NonFiction As String

Private Sub btnRecord_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord.Click


    ''Read the information into a string''
    Dim objReader As New StreamReader("C:\Users\Books.txt")
    Dim fileString As String = objReader.ReadToEnd()
    objReader.Close()
    objReader.Dispose()

    ''Concert string into array''
    Dim Array As Char() = fileString.ToCharArray()
    Array(0) = CChar("BookName")

    ''Declare the string to hold the value''
    Dim resultString As String = ","

    ''Increment each item in the string''
    Dim curChar As Char
    For i As Integer = 0 To (Array.Length - 1)
        curChar = Array(i)
        If (Char.IsLetterOrDigit(curChar)) Then
            Array(i) = Chr(Asc(curChar) + 1)
        End If
    Next

    ''Conver the array back to string''
    Dim newString As String = New String(Array)

    ''Hide the details form when book inventory runs''
    Me.Hide()

    ''display inventory form''
    frmInventory.Show()

End Sub

推荐答案

为了说明,用户输入

将标题变暗为字符串将作者作为字符串变暗Dim Stock 作为整数暗淡价格双倍暗淡的小说作为字符串Dim NonFiction As String

Dim Title As String Dim Author As String Dim Stock As Integer Dim Price As Double Dim Fiction As String Dim NonFiction As String

你然后你搜索数据做文件C:\Users\Books.txt"?

You then you search for the data do the file "C:\Users\Books.txt"?

因为,假设您是将所有书籍添加到文本文件中的人,并假设您将每本书放在具有相同属性的行中,即

Because, assuming you are the one who has added all the books to the text file, and assuming that you put each book on its own line with the same properties, i.e.

书名、作者、价格等下一本书,......等等

book title, author, price, etc next book, .... and so on

那我不明白你为什么不做这样的事情...

then I don't see why you wouldn't do something like this...

  'Storing All Book Information Into One Object
   Public Structure BookDetails
       Dim Title As String
       Dim Author As String
       Dim Stock As Integer
       Dim Price As Double
       Dim Fiction As String
       Dim NonFiction As String
     End Structure
   sub ... 
    Dim theObjContents as new ArrayList
   Using objReader as new StreamReader("C:\Users\Books.txt")


    while objReader.endofstream = false
      theObjContents.add(objReader.readline)
    end while

   End Using        

    For each line in theObjContents
   'Check what you want
       if bookdetails.Title = line.split(whatever your delimiting character is").item(0)

    next
 end sub

我不确定这是否能回答任何问题,如果我完全不了解您想要什么,请告诉我.

I am not sure if that answers anything at all, please let me know if I am totally off on what you wanted.

这篇关于从文本文件中读取信息并显示在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆