vb 列表框 的问题

来源:百度知道 编辑:UC知道 时间:2024/07/06 20:52:26
Private Sub Command1_Click()
If List1.Text = "苹果" Then
List2.AddItem "苹果"
a = 5
End If
s = s + a
If List1.Text = "梨" Then
List2.AddItem "梨"
b = 4
End If
s = s + b
Label1.Caption = s
End Sub

如以上程序,我本来是想,在列表框1中列举出很多种水果名称,然后用添加按钮将选中的项目添加到列表框2中(使用单选),最后计算出添加到列表框2中的水果的价格(a,b为苹果和梨的价格)。但是最后输出显示的时候,他只显示我在列表框1中所选择的最后一种水果的价格,而不是之前所选价格的总和,哪位大侠指教下该怎么改。
谢谢!

你前后需要点两个按钮,而每一次只执行一个改变S值的。所以根本原来还是在于你的变量S是局部变量,或者你无意在哪地方让S清零了。
如果是局部变量,你只要将S定义为全局变量即可。
如果是全局变量,你要将给S清零的语句加在合适的位置,就是什么时候价格需要清零的位置。

Private Static Sub Command1_Click()

If List1.Text = "苹果" Then
List2.AddItem "苹果"
a = 5
S = S + a
End If
If List1.Text = "梨" Then
List2.AddItem "梨"
b = 4
S = S + b
End If
Label1.Caption = S
End Sub

你是要多选的话,就不能用list1.text属性了。
因为list1.text只读出一行的内容,当然你的答案就只有最后一条了。
一点都不奇怪。
如果不知道怎么写,hi我。

Dim s as single

Private Sub Command1_Click()
If List1.Text = "苹果" Then
List2.AddItem "苹果"
a = 5
End If
s = s + a
If List1.Text = "梨" Then
List2.AddItem "梨"
b = 4
End If
s = s + b
Label1.Caption = s
End Sub