vb 实时错误'381'

来源:百度知道 编辑:UC知道 时间:2024/06/17 00:38:53
程序第一次运行没问题,但第二次运行就提示381错误.程序目的是:在单击list1中的时候,将其值设为list2的值.程序如下;
Dim ab As Long
Dim ac As String
ab = List1.ListIndex '返回当前选择的list1的index值
ac = List1.List(ab) '获取当前选择的list1的list值
List2.List(ab) = ac

楼上是对的,但会产出重复,要解决重复问题,我将你的代码改成如下:

Dim ab As Integer
Dim ac As String
Dim CurListStr As Boolean '检查有无重复(即下百List2中是否存在List1中的值)

ab = List1.ListIndex
ac = List1.List(ab)
For i = 0 To List2.ListCount - 1
If ac = List2.List(i) Then
CurListStr = True
End If
Next
If CurListStr = False Then
List2.List(List2.ListCount) = ac
End If
CurListStr = False '记得复位

主要是索引值混乱的问题,改成下面的这样就可以随意添加了!

Dim ab As Long
Dim ac As String
ab = List1.ListIndex '返回当前选择的list1的index值
ac = List1.List(ab) '获取当前选择的list1的list值
List2.List(List2.ListCount) = ac