VB中列表框List赋值并在文本框输出
Dim Player(0 To 2) ' 说明两个数组的大小。
Dim Salary(0 To 2)
Dim a As Long
Dim b As Long
Dim c As Long
Dim sum As String
Private Sub Command1_Click()
List2.AddItem List1.List(List1.ListIndex)
List1.RemoveItem List1.ListIndex
End Sub
Private Sub Form_Load()
Dim I ' 声明变量。
AutoSize = True
Player(0) = "背带裤" ' 在数组中输入数据。
Player(1) = "毛衣"
Player(2) = "卫衣"
Salary(0) = "165"
Salary(1) = "135"
Salary(2) = "75"
a = Salary(0)
b = Salary(1)
c = Salary(2)
For I = 0 To 2 ' 在列表中添加名字。
List1.AddItem Player(I)
Next I
List1.ListIndex = 0 ' 显示列表中的第一项。
End Sub
Private Sub Combo1_Click()
End Sub
Private Sub List2_Click()
sum = a + b + c
' 显示名字所对应的价格。
Text1.Text = sum + "元"
End Sub
这样写只能输入三个的总和、怎么达到输入一个商品只显示那个商品的、求啊……有20分 展开
看来一直没人为你解决,我给你将代码修改了一下,你复制过去运行即可
你的窗体上应该具备以下4个控件:List1、List1、Text1和Command1 【属性默认】
Dim Player(0 To 2) As String ' 说明两个数组的大小。
Dim Salary(0 To 2) As Single
Dim sum As Single
Private Sub Command1_Click()
Dim i As Long, j As Long ' 声明变量。
If List1.ListCount = 0 Then Exit Sub
If List1.ListIndex < 0 Then
MsgBox "请在列表框1中选择一个商品"
Exit Sub
End If
List2.AddItem List1.List(List1.ListIndex)
List1.RemoveItem List1.ListIndex
If List2.ListCount > 0 Then
sum = 0
For i = 0 To List2.ListCount - 1
For j = 0 To 2
If List2.List(i) = Player(j) Then sum = sum + Salary(j)
Next j
Next i
Else
Text1.Text = "0元"
End If
Text1.Text = CStr(sum) + "元" ' 显示名字所对应的价格。
End Sub
Private Sub Form_Load()
Dim i As Long ' 声明变量。
AutoSize = True
Player(0) = "背带裤" ' 在数组中输入数据。
Player(1) = "毛衣"
Player(2) = "卫衣"
Salary(0) = 165
Salary(1) = 135
Salary(2) = 75
For i = 0 To 2 ' 在列表中添加名字。
List1.AddItem Player(i)
Next i
List1.ListIndex = 1 ' 显示列表中的第一项。
End Sub
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
高手可以的话你帮我解决上面的问题吧……