注册 登录
编程论坛 VB6论坛

vb 下标越界怎么解决?

ymhy12345 发布于 2012-12-27 13:47, 11639 次点击
用的是listbox控件,源码
程序代码:
Dim AssStations() As String
Private Sub Form_Load()
Dim Tmp1, Tmparray1

 ReDim AssStations(0) As String

 If Dir(App.Path & "\adult.dat") <> "" Then
    Open App.Path & "\adult.dat" For Input As #1
        aLista.Clear
        While Not EOF(1)
        Line Input #1, Tmp1
        If Trim(Tmp1) <> "" Then
            Tmparray1 = Split(Tmp1, "||>")
            ReDim Preserve AssStations(UBound(AssStations) + 1) As String
            If Tmparray1(0) <> "" Then
                AssStations(UBound(AssStations) - 1) = Trim(Tmparray1(1)) '下标越界,错误提示来自这一行。怎么解决
                aLista.AddItem Trim(Tmparray1(0))
            End If
        End If
    Wend
Close #1
End If


End Sub

只有本站会员才能查看附件,请 登录


[ 本帖最后由 ymhy12345 于 2012-12-27 13:50 编辑 ]
8 回复
#2
风吹过b2012-12-27 14:08
Dim AssStations() As String
Private Sub Form_Load()
Dim Tmp1, Tmparray1()
ReDim AssStations(0) As String
If Dir(App.Path & "\adult.dat") <> "" Then
 Open App.Path & "\adult.dat" For Input As #1
愀Lista.Clear
圀栀椀氀攀 Not EOF(1)
Line Input #1, Tmp1
If Trim(Tmp1) <> "" Then
吀洀瀀愀爀爀愀礀1 = Split(Tmp1, "||>")
刀攀Dim Preserve AssStations(UBound(AssStations) + 1) As String
'If Tmparray1(0) <> "" Then
            'If ubound(Tmparray1)> 0 and Tmparray1(0) <> ""  Then             '确保有二个元素,并且第一个元素不为空
            If ubound(Tmparray1)> 0 Then          '确保有二个元素
 AssStations(UBound(AssStations) - 1) = Trim(Tmparray1(1)) '下标越界,错误提示来自这一行。怎么解决
 aLista.AddItem Trim(Tmparray1(0))
End If
End If
 Wend
Close #1
End If
End Sub

-----------
当读到的数据不为空时,并且不带 "||>" 时,
会导致 Tmparray1 的最大下标只有 0 。
#3
风吹过b2012-12-27 14:09
复制一下,再编辑一下,就出乱码啦。自己慢慢看吧。只改了变色的地方。
#4
ymhy123452012-12-27 15:43
回复 3楼 风吹过b
好像还是不行啊,加载记事本还是出现下标越界
整个源码在这
只有本站会员才能查看附件,请 登录
#5
yz10252012-12-27 16:19
程序代码:

Dim AssStations() As String

Private Sub aLista_Click()
wmp1.URL = AssStations(aLista.ListIndex)
End Sub

Private Sub Form_Load()
Dim Tmp1, Tmparray1
Dim i As Integer

    i = 0: ReDim AssStations(i)
   
    If Dir(App.Path & "\adult.dat") <> "" Then
        Open App.Path & "\adult.dat" For Input As #1
            aLista.Clear
            While Not EOF(1)
                Line Input #1, Tmp1
                If Trim(Tmp1) <> "" Then
                    If InStr(Tmp1, "||>") <> 0 Then    '<------ P.S
                        Tmparray1 = Split(Tmp1, "||>")
                        ReDim Preserve AssStations(i)
                        If Tmparray1(0) <> "" Then
                            AssStations(i) = Trim(Tmparray1(1))
                            aLista.AddItem Trim(Tmparray1(0))
                        End If
                        i = i + 1
                    End If
                End If
            Wend
        Close #1
    End If
   
End Sub
#6
ymhy123452012-12-27 17:28
回复 5楼 yz1025
谢谢各位版主,加载记事本可以了,就是加载后<<<分区一>>>这些文字没有显出来
#7
yz10252012-12-27 20:11
回复 6楼 ymhy12345
这你没讲我没看楼上的文不知道
这只需加个Else就可以做到
让你当作业
#8
ymhy123452012-12-27 22:25
俺加一行On Error Resume Next
没有显示下标越界,加载记事本,列表也显示分区一了
#9
ymhy123452012-12-27 23:55
回复 7楼 yz1025
不加这个on error resume next,俺还真没办法解决了,俺对listbox不太懂
1