2014-11-22 108 views
0

我被卡住了,並試圖多次重寫我的代碼,並找不到解決方案。該應用程序具有包含順序訪問文件中的項目和價格的文本文件。從列表框中選擇應用時,應用程序應顯示與該項目對應的價格。文本文件中的每一行都包含該項目的編號,後跟一個逗號,然後是價格。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 
+0

你忘了增加'我ntSub'。 – 2014-11-22 22:14:19

+0

另請參閱http://stackoverflow.com/a/27080237/1070452 – Plutonix 2014-11-22 22:16:18

回答

0

我認爲你需要改變結構的名稱。項目可以在另一個參考中使用。

嘗試將名稱更改爲itemstr(例如)

 Do Until inFile.Peek = -1 OrElse 
     intSub = items.Length 
     strLineofText = inFile.ReadLine.Trim 
     'add item to list box 
     Dim arr As String() = str.Split(","C) 
     Dim valitem As New itemstr() 
     valitem.text = arr(0) 
     valitem.value = Convert.ToDecimal(arr(1)) 
     lstNumbers.Items.Add(valitem) 
    Loop 
0

謝謝大家。我最終重新開始,嘗試一些不同的東西,終於有效了!

顯式的選項在 選項嚴格在 選項推斷關

公共類frmMain 「聲明結構 結構項目 公共strItemNum作爲字符串 公共decPrice爲十進制 末端結構

'declare class level array 
Public itemList(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 decPrice As Decimal 
    Dim strInfo(4, 1) As String 

    'check to see if the file exists 
    If IO.File.Exists("ItemInfo.txt") Then 
     'open the file 
     inFile = IO.File.OpenText("ItemInfo.txt") 

     For intRow As Integer = 0 To strInfo.GetUpperBound(0) 
      'read the line 
      strLineOfText = inFile.ReadLine 

      'assign substring from comma to first coloumn 
      strInfo(intRow, 0) = strLineOfText.Substring(0, strLineOfText.IndexOf(",")) 

      Dim intSep As Integer = strLineOfText.IndexOf(",") + 1 

      'assign substring after comma to 2nd column 
      strInfo(intRow, 1) = strLineOfText.Substring(intSep) 

      'assign first column value to strItemNum variable 
      itemList(intRow).strItemNum = (strInfo(intRow, 0)) 

      Decimal.TryParse(strInfo(intRow, 1), decPrice) 

      'assign 2nd columnn value to decPrice variable 
      itemList(intRow).decPrice = decPrice 


      'add item to listbox 
      lstNumbers.Items.Add(itemList(intRow).strItemNum) 
     Next intRow 

     'clost the file 
     inFile.Close() 

    Else 
     'error message if file cannot be found 
     MessageBox.Show("Can't find the ItemInfo.txtfile", 
         "Kensington Industries", 
         MessageBoxButtons.OK, 
         MessageBoxIcon.Information) 

    End If 
End Sub 

Private Sub lstNumbers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lstNumbers.SelectedIndexChanged 
    'display the price 
    lblPrice.Text = itemList(lstNumbers.SelectedIndex).decPrice.ToString("C2") 
End Sub 

結束