0

VB.Net: I am using two forms - frmDetails, frmInventory frmInventory contains a list box that reads a text file containing a list of book title, author, category, # in stock, and price per item (five elements). The list box displays only the book titles.

frmDetails contains individual text boxes that match the elements in the text file.

If a user selects one of the items (titles) in the frmInventory list box and chooses update from a drop-down menu, the text boxes in frmDetails need to be populated with the elements that match their label (title with title, author with author, etc.). In other words, after selected, the text file needs to be read, the data parsed, and populated in each text box.

I have tried a few different forms of code:

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

Or:

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

Or:

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

The problem is with parsing the data in the record so that it can be dispersed into the different text fields.

Any assistance would be appreciated.

4

1 に答える 1

0

OK、まあ、本のクラスを作成し、テキストファイルをある種のコレクション(本の)に読み込む必要があるという最初のコメントを支持します。

要件に固有の場合、バインディングリストが適切です。

例:

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 に設定します。また、キャンセル ボタンのプロパティを適切なボタンに設定する必要があります。

最後に book クラス自体:

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 に答える