0

VB.Net:我使用两种形式——frmDetails、frmInventory frmInventory 包含一个列表框,它读取一个文本文件,其中包含书名、作者、类别、库存编号和每件商品价格(五个元素)的列表。列表框仅显示书名。

frmDetails 包含与文本文件中的元素匹配的单个文本框。

如果用户在 frmInventory 列表框中选择其中一项(标题)并从下拉菜单中选择更新,则 frmDetails 中的文本框需要填充与其标签匹配的元素(标题与标题,作者与作者, ETC。)。换句话说,选择后,需要读取文本文件,解析数据,并填充到每个文本框中。

我尝试了几种不同形式的代码:

Dim selectedUpdate As String = lstBookList.SelectedItem.ToString
If selectedUpdate = lstBookList.SelectedItem.ToString Then
  Dim queryUpdate = From item In File.ReadAllLines("Books.txt")
                    Let ti = item.Split(","c)(0)
                    Let au = item.Split(","c)(1)
                    Let ca = item.Split(","c)(2)
                    Let qt = item.Split(","c)(3)
                    Let co = item.Split(","c)(4)
                    Where ti = selectedUpdate
                    Select ti & "," & au & "," & ca & "," & qt & "," & co
  For Each ti In queryUpdate
    frmDetails.txtTitle.Text = ti
    For Each au In queryUpdate
      frmDetails.txtAuthor.Text = au
      For Each qt In queryUpdate
        frmDetails.txtStock.Text = qt
        For Each co In queryUpdate
          frmDetails.txtPrice.Text = co
        Next
      Next
    Next
  Next
End If
frmDetails.ShowDialog()
End Sub

或者:

Dim selectedUpdate As String = lstBookList.SelectedItem.ToString
Dim itemToUpdate() As String = File.ReadAllLines("Books.txt")
If selectedUpdate = lstBookList.SelectedItem.ToString Then
Dim queryTitle = From bookTitle In itemToUpdate
                   Let ti = bookTitle.Split(","c)(0)
                   Where ti = selectedUpdate
                   Select ti
  For Each ti In queryTitle
    frmDetails.txtTitle.Text = ti
  Next
  Dim queryAuthor = From bookAuthor In itemToUpdate
                   Let au = bookAuthor.Split(","c)(1)
                   Where au = selectedUpdate
                   Select au
  For Each au In queryAuthor
    frmDetails.txtAuthor.Text = au
  Next

或者:

Dim selectedUpdate As String = lstBookList.SelectedItem.ToString
Dim itemToUpdate() As String = File.ReadAllLines("Books.txt")
If selectedUpdate = lstBookList.SelectedItem.ToString Then
  Dim queryUpdate = From item In File.ReadAllLines("Books.txt")
                   Let ti = item.Split(","c)(0)
                   Let au = item.Split(","c)(1)
                   Let ca = item.Split(","c)(2)
                   Let qt = item.Split(","c)(3)
                   Let co = item.Split(","c)(4)
                   Where ti = selectedUpdate
                   Select ti, au, ca, qt, co
  frmDetails.txtTitle.Text = (queryUpdate.ToString) ti
  frmDetails.txtAuthor.Text = au
  frmDetails.txtStock.Text = qt

问题在于解析记录中的数据,以便将其分散到不同的文本字段中。

任何援助将不胜感激。

4

1 回答 1

0

好的,我坚持我最初的评论,即您应该创建一类书籍并将文本文件读入某种(书籍)集合中。

具体到您的要求,绑定列表将是合适的。

一个例子:

Public Class frmInventory
Public bookList As New BindingList(Of Book)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Presumes each book is stored on one line, with data seperated with a ";"
    'eg: Wrox Beginning Visual Basic 2010;Thearon Willis;non-fiction;100;17.5
    Dim books As String() = IO.File.ReadAllLines("C:\Users\Admin\Documents\allBooks.txt")

    For i = 0 To books.Count - 1
        Dim bookdata As String() = books(i).Split(";")
        bookList.Add(New Book(bookdata(0), bookdata(1), bookdata(2), CInt(bookdata(3)), CDbl(bookdata(4))))
    Next

    'set listbox1 datasource to our newly populated bindinglist(of book)
    ListBox1.DataSource = bookList
    'set displaymember to title. Could be any one of the other attributes of book.
    ListBox1.DisplayMember = "title"
End Sub

'When edit button is clicked: if you want the following to fire when an option is selected from a dropdown, handle accordingly
Private Sub btnEditSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditSelected.Click

    'the listbox contains book objects, not a string! This can catch people out
    Dim selectedBook As Book = ListBox1.SelectedItem
    'create an instance of frmDetails, and pass it the selected book object
    Dim editform As New frmDetails(selectedBook)
    'Show editform as model and if the user clicks the OK button on the model form:
    If editform.ShowDialog() = DialogResult.OK Then
        'replace the existing book object in booklist with the modified one.
        bookList(bookList.IndexOf(selectedBook)) = selectedBook
    End If

End Sub


End Class

您的 frmDetails 看起来像这样:

Public Class frmDetails
'auto property: creates a private book object called _book (note the underscore), see
'http://msdn.microsoft.com/en-us/library/dd293589.aspx for more details on autoproperties
Property book As Book

'require a book object to be sent when a new instance is created, by reference so we can edit the actual book object, not a copy
Public Sub New(ByRef book As Book)

    _book = book

    InitializeComponent()

End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = _book.title
    TextBox2.Text = _book.author
    TextBox3.Text = _book.category
    TextBox4.Text = _book.stock
    TextBox5.Text = _book.wholesaleprice
End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    _book.title = TextBox1.Text
    _book.author = TextBox2.Text
    _book.category = TextBox3.Text
    _book.stock = TextBox4.Text
    _book.wholesaleprice = TextBox5.Text
    DialogResult = DialogResult.OK
End Sub
End Class

在设计器中,将表单的“AcceptButton”属性设置为 btnOK。您还应该将其取消按钮属性设置为适当的按钮

最后是书类本身:

Public Class Book
Property title As String
Property author As String
Property category As String
Property stock As Integer
Property wholesaleprice As Double

Public Sub New(ByVal title As String, ByVal author As String, ByVal category As String, ByVal stock As Integer, ByVal wholesaleprice As Double)

    _title = title
    _author = author
    _category = category
    _stock = stock
    _wholesaleprice = wholesaleprice
End Sub
End Class
于 2012-11-26T09:54:15.037 回答