到目前为止,我有这个,虽然我遗漏了一些东西。我应该使用数组来填充列表框中的.txt中的项目,然后当单击列表框中的项目时,标签会以价格更新。有什么建议吗?
Public Class frmMain
Structure product
Public itemnumber As String
Public itemprice As Decimal
End Structure
Private Sub ButtonExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ItemInfo As String = "H:\Users\user\Documents\Visual Studio 2012\Projects\WindowsApplication9\ItemInfo.txt"
Dim inFile As IO.StreamReader
Dim strName As String
Dim skipLine As Boolean
If System.IO.File.Exists("H:\Users\user\Documents\Visual Studio 2012\Projects\WindowsApplication9\ItemInfo.txt") = True Then
inFile = IO.File.OpenText("H:\Users\user\Documents\Visual Studio 2012\Projects\WindowsApplication9\ItemInfo.txt")
Do Until inFile.Peek = -1
strName = inFile.ReadLine
If skipLine = False Then
ListBox1.Items.Add(strName)
skipLine = True
Else : skipLine = False
End If
Loop
Dim objReader As New System.IO.StreamReader(ItemInfo)
ListBox1.Text = objReader.ReadToEnd
objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
End Sub
End Class
答案 0 :(得分:0)
这是一个很大的提示:
Imports System.IO
Public Class Form1
Dim myItems As List(Of Product)
Class Product
Public ItemNumber As String
Public ItemPrice As Decimal
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim itemInfo As String = "H:\Users\user\Documents\Visual Studio 2012\Projects\WindowsApplication9\ItemInfo.txt"
If File.Exists(itemInfo) Then
myItems = New List(Of Product)
Dim allItems = File.ReadAllLines(itemInfo)
For Each line As String In allItems
' see http://msdn.microsoft.com/en-us/library/ms131448%28v=vs.110%29.aspx for String.Split
Dim parts() As String = line.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
' only process lines with two parts
If parts.Count = 2 Then
Dim itemEntry As New Product
itemEntry.ItemNumber = parts(0)
'TODO: (optional) use Decimal.TryParse to make sure the entry really can be parsed as a Decimal.
itemEntry.ItemPrice = CDec(parts(1))
myItems.Add(itemEntry)
End If
Next
Else
'HINT: always give as much info as is reasonable when saying something went wrong.
MsgBox("File """ & itemInfo & """ does not exist or could not be opened.")
End If
'TODO: show the items by getting the .ItemNumber of each item in myItems
End Sub
End Class