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
问题在于解析记录中的数据,以便将其分散到不同的文本字段中。
任何援助将不胜感激。