请人解说一下这段VB代码,我新手,30分哦!!

来源:百度知道 编辑:UC知道 时间:2024/06/08 19:13:18
Private Sub CmdRight_Click()
Dim X As Integer
While LstLeft.SelCount > 0
For X = 0 to LstLeft.LstCount - 1
If LstLeft.Selected(X) = True Then
LstRight.AddItem LstLeft.List(X)
LstLeft.RemoveItem X
Exit For
End If
Next X
Wend
End Sub

特别是For X = 0 to LstLeft.LstCount - 1
和为什么先next for 再end if
为什么For X = 0 to LstLeft.LstCount - 1 没有step

问题补充:为什么For X = 0 to LstLeft.LstCount - 1 没有step
没有step默认为1

Private Sub CmdRight_Click()
Dim X As Integer
While LstLeft.SelCount > 0 '当LstLeft被选中数量大于零(选中了值)开始While循环
For X = 0 To LstLeft.LstCount - 1 '对LstLeft中的项进行循环判断
If LstLeft.Selected(X) = True Then '判断是否选中
LstRight.AddItem LstLeft.List(X) '如选中,添加至LstRight
LstLeft.RemoveItem X '并在LstLeft删除它
Exit For '跳出for循环(如果多选了值,还将继续执行for,因为LstLeft.SelCount将仍>0)
'此处是跳出,不是退出,只在LstLeft.Selected(X) = True条件成立的情况下才跳出
'每次跳出执行效率有点低,不过是防止错误的一个办法
End If
Next X '如未中途跳出,此处当X>=LstLeft.LstCount - 1 时才会退出for循环
Wend
End Sub

步长为1时省略step

循环从0到listcount-1是指从列表的第一项开始判断一直到列表的最后一项,从1到listcount也可!默认的列表首项index值是0而不是1
for next循环与if条件判断不能套用比如:
for ……
if
……
next
end if(这是错误的)

但是你给出的代码可以,即设置条件Exit for(退出循环)再结束条件句就不会出现套用情况!