使用从列表框中选择的文本文件中的数据填充第二个表单

时间:2012-11-25 20:39:39

标签: vb.net vb.net-2010

VB.Net: 我使用两种形式 - frmDetails,frmInventory frmInventory包含一个列表框,其中包含一个文本文件,其中包含书名,作者,类别,库存中的#和每个项目的价格(五个元素)列表。列表框仅显示书名。

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

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

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

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

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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