我被卡住了,并试图多次重写我的代码,并找不到解决方案。该应用程序具有包含顺序访问文件中的项目和价格的文本文件。从列表框中选择应用时,应用程序应显示与该项目对应的价格。文本文件中的每一行都包含该项目的编号,后跟一个逗号,然后是价格。Visual Basic问题获取列表框从文本文件填充
我需要定义一个名为item的结构。该结构具有2个成员变量,一个用于存储项目号的字符串和一个用于存储价格的小数。我还需要用5个项目结构变量声明一个类级别的数组。负载甚至应该读取项目和价格,并将这些信息存储在类级数组中。然后它应该将项目号码添加到列表框中。
这是我迄今为止,但没有任何工作。
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
'declare structure with 2 member variables
Structure Item
Public strItemNum As String
Public decPrice As Decimal
End Structure
'declare array for 5 item structure variables
Private items(4) As Item
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
'declare variables
Dim inFile As IO.StreamReader
Dim strLineofText As String
Dim intSub As Integer
'check if the file exists
If IO.File.Exists("ItemInfo.txt") Then
'open the file
inFile = IO.File.OpenText("ItemInfo.txt")
'read the file
Do Until inFile.Peek = -1 OrElse
intSub = items.Length
strLineofText = inFile.ReadLine.Trim
'add item to list box
lstNumbers.Items.Add(items(intSub).strItemNum)
Loop
'close the file
inFile.Close()
Else
MessageBox.Show("Can't find the ItemInfo.txtfile",
"Kensington Industries",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
Private Sub lstNumbers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstNumbers.SelectedIndexChanged
lblPrice.Text = items(lstNumbers.SelectedIndex).decPrice.ToString("C2")
End Sub
End Class
你忘了增加'我ntSub'。 – 2014-11-22 22:14:19
另请参阅http://stackoverflow.com/a/27080237/1070452 – Plutonix 2014-11-22 22:16:18