+ -
当前位置:首页 → 问答吧 → 请教一下这个数组要怎么清空?

请教一下这个数组要怎么清空?

时间:2011-10-24

来源:互联网

按理说用erase 就可以清空数组,可是我这个代码按下command1明显数组I没有被清空,不断按下则list1不断地叠加.重启程序才能清空.

要怎么清空呢?谢谢!!

完整代码如下:
VB code
Private Sub Command1_Click()
Dim I() As String, J As Long
List1.Clear

I = SearchFileInPath("C:\Users\kang\Desktop\test", "*.txt")'调用模块搜索得到文件夹下所有txt后缀名的文件名称
For J = 0 To UBound(I)
    List1.AddItem I(J)
Next

Erase I'我想在把文件名放入list1之后I就清空,不然我换个文件夹搜的话上面搜到的文件名还会在那就不对了
End Sub



这个是人家高人的一个搜索模块,我上面是直接调用了这个:
VB code
Option Explicit
Private FoundFile() As String '存放传回值的字串阵列
Private Ntx As Long

Public Function SearchFileInPath(ByVal thePath As String, ByVal theFileName As String, Optional ByVal mStop As Boolean = False) As String()
    '使用递归方式搜索文件
    'thePath - 要搜索的目录
    'theFileName - 文件名,支持通配符
    'mStop - T=找到一个就返回,F=返回所有找到的文件
    '返回值:
    '       搜索到的文件
    If Right(thePath, 1) <> "\" Then thePath = thePath & "\"
    Call GetFileLoop(thePath, theFileName, mStop)
    SearchFileInPath = FoundFile
End Function

Private Function GetFileLoop(CurrentPath As String, ByVal SearFile As String, Optional ByVal mStop As Boolean = False) As String
    Dim nI As Integer, nDirectory As Integer, I As Long
    Dim sFileName As String, sDirectoryList() As String
    
    On Error Resume Next
    sFileName = Dir(CurrentPath, vbHidden Or vbDirectory Or vbReadOnly Or vbSystem)
    Do While sFileName <> ""
        If UCase(sFileName) Like UCase(SearFile) Then
            I = GetAttr(CurrentPath + sFileName)
            If (I And vbDirectory) = 0 Then
                If mStop = False Then
                    ReDim Preserve FoundFile(Ntx)
                    FoundFile(Ntx) = CurrentPath + sFileName
                    Ntx = Ntx + 1
                Else
                    GetFileLoop = CurrentPath + sFileName
                    Exit Function
                End If
            End If
        End If
        If sFileName <> "." And sFileName <> ".." Then
            If GetAttr(CurrentPath & sFileName) _
            And vbDirectory Then
                
                nDirectory = nDirectory + 1
                ReDim Preserve sDirectoryList(nDirectory)
                sDirectoryList(nDirectory) = CurrentPath & sFileName
            End If
        End If
        sFileName = Dir
    Loop
    For nI = 1 To nDirectory
         GetFileLoop = GetFileLoop(sDirectoryList(nI) & "\", SearFile)
         If GetFileLoop <> "" And mStop = True Then Exit For
    Next nI
End Function

作者: kangxiangna   发布时间: 2011-10-24

i是过程内的局部变量,不用清空,下次点击按钮的时候就是空的
手关没有VB6没法试,我觉得是你的List1.Clear并没有达到清空List的效果

作者: alifriend   发布时间: 2011-10-24

谢谢楼上大侠.但是这里可能有点混淆:
那个i在模块里有,但是在command1的过程里又有,这里可能有点混淆了.我指的是command1过程里面那个I不能清空.模块里的那个i是无关的.
list1这样可以清空的吧?

另外我觉得我这里command1不断按下的效果应该是list1中始终都是一样的.但是现在的效果是不断地累加了.

作者: kangxiangna   发布时间: 2011-10-24

为了防止误解i,我重新写了下,把它改成k了:但是还是不行,k还是不能清空,会不断重复累加.
VB code
Private Sub Command1_Click()
Dim k() As String, J As Long
List1.Clear

k = SearchFileInPath("c:\Users\kang\Desktop\test", "*.txt")
For J = 0 To UBound(k)
    List1.AddItem k(J)
Next

Erase k

End Sub

作者: kangxiangna   发布时间: 2011-10-24

累加并不是list1没有清空造成的,我加了一个清空按钮测试过,手工清空,然后按下command1,得出的还是累加的

Private Sub Command2_Click()
List1.Clear
End Sub

作者: kangxiangna   发布时间: 2011-10-24