建一個TXT檔案,名為:ShangPin.txt,內容為:
40寸彩電,4800
夏普空調,3680
電風扇,182
腳踏車,555
新建工程,儲存到某資料夾,把上面的TXT檔案也複製到工程資料夾裡。
在窗體上新增2個列表框,1個文字框,把列表框1的Style屬性設定為1。
程式碼如下:
Option Explicit
Private SPM() As String
Private DJ() As Single
Private Sub Form_Load()
"讀取檔案資訊
Dim MyStr As String
Dim n As Integer
Dim L As Integer
List1.Clear
n = -1
Open App.Path & "\ShangPin.txt" For Input As #1 "以讀的方式開啟檔案
Do While Not EOF(1) " 迴圈至檔案尾
Line Input #1, MyStr "讀入一行
n = n + 1
ReDim Preserve SPM(n) As String
ReDim Preserve DJ(n) As Single
L = InStr(MyStr, ",")
SPM(n) = Left(MyStr, L - 1)
List1.AddItem SPM(n)
MyStr = Right(MyStr, Len(MyStr) - L)
DJ(n) = MyStr
Loop
Close #1 " 關閉檔案。
List2.Clear
End Sub
Private Sub List1_Click()
Dim i As Integer
Dim Total As Single
Total = 0
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
Total = Total + DJ(i)
List2.AddItem List1.List(i)
End If
Next i
Text1.Text = CStr(Total)
建一個TXT檔案,名為:ShangPin.txt,內容為:
40寸彩電,4800
夏普空調,3680
電風扇,182
腳踏車,555
新建工程,儲存到某資料夾,把上面的TXT檔案也複製到工程資料夾裡。
在窗體上新增2個列表框,1個文字框,把列表框1的Style屬性設定為1。
程式碼如下:
Option Explicit
Private SPM() As String
Private DJ() As Single
Private Sub Form_Load()
"讀取檔案資訊
Dim MyStr As String
Dim n As Integer
Dim L As Integer
List1.Clear
n = -1
Open App.Path & "\ShangPin.txt" For Input As #1 "以讀的方式開啟檔案
Do While Not EOF(1) " 迴圈至檔案尾
Line Input #1, MyStr "讀入一行
n = n + 1
ReDim Preserve SPM(n) As String
ReDim Preserve DJ(n) As Single
L = InStr(MyStr, ",")
SPM(n) = Left(MyStr, L - 1)
List1.AddItem SPM(n)
MyStr = Right(MyStr, Len(MyStr) - L)
DJ(n) = MyStr
Loop
Close #1 " 關閉檔案。
List2.Clear
End Sub
Private Sub List1_Click()
Dim i As Integer
Dim Total As Single
Total = 0
List2.Clear
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
Total = Total + DJ(i)
List2.AddItem List1.List(i)
End If
Next i
Text1.Text = CStr(Total)
End Sub