注册 登录
编程论坛 VB6论坛

请大大们看看,出了什么问题

tyms 发布于 2012-10-14 11:28, 502 次点击
Private Sub fn_Open_Click()
  Dim I As Long, b As String, a() As String
    CommonDialog1.ShowOpen             'Show file Open Dialog
    fn = CommonDialog1.FileName        'Get the Chosen File Name
    If fn = "" Then Exit Sub    'Make Sure User have selected a file
         Form1.ListView1.ListItems.Clear
        Open fn For Input As #1
         For I = 1 To GetCountRow(fn)
            ReDim a(3)
            Input #1, b
            a = Split(b, ",")
             ListView1.ListItems.add , "", I
            ListView1.ListItems(I).SubItems(1) = a(0)
            ListView1.ListItems(I).SubItems(2) = a(1)
            ListView1.ListItems(I).SubItems(3) = a(2)
            ListView1.ListItems(I).SubItems(4) = a(3)
            ListView1.ListItems(I).SubItems(5) = "1"
            DoEvents
        Next
End Sub
这是我编的一D代码,文件中的数据是
"5637438","531","179","500"
"5637438","613","169","500"
...
"5637438","454","243","500"
现在我把数取出来放到listview1中,为什么到了ListView1.ListItems(I).SubItems(2) = a(1)
出现下标越界
请指教,能够给出完成的代码更好

7 回复
#2
tyms2012-10-14 11:30
补充 GetCountRow(fn)是取文件多少行函数
#3
bczgvip2012-10-14 14:06
Private Sub fn_Open_Click()
  Dim I As Long, b As String, a() As String
    CommonDialog1.ShowOpen             'Show file Open Dialog
    fn = CommonDialog1.FileName        'Get the Chosen File Name
    If fn = "" Then Exit Sub    'Make Sure User have selected a file
         Form1.ListView1.ListItems.Clear
        Open fn For Input As #1
         For I = 1 To GetCountRow(fn)
            ReDim a(3)
            Input #1, b
            a = Split(b, ",")
            dim litem as listitem
            set litem= ListView1.ListItems.add( , , I)
            litem.SubItems(1) = a(0)
            litem.SubItems(2) = a(1)
            litem.SubItems(3) = a(2)
            litem.SubItems(4) = a(3)
            litem.SubItems(5) = "1"
            DoEvents
        Next
End Sub
'没测试自己试试吧!
#4
tyms2012-10-14 14:23
Private Sub fn_Open_Click()
  Dim I As Long, b As String, a() As String, PP As Integer
    CommonDialog1.ShowOpen             'Show file Open Dialog
    fn = CommonDialog1.FileName        'Get the Chosen File Name
    If fn = "" Then Exit Sub    'Make Sure User have selected a file
         ListView1.ListItems.Clear
       Open fn For Input As #1‘我在你基础上加了这三行
       PP = GetCountRow(fn)    ‘
       Close #1                 ‘
         Open fn For Input Access Read Lock Read As #1
         For I = 1 To PP
            ReDim a(3)
            Input #1, b
            a = Split(b, ",")
           Dim litem As ListItem
            ListView1.ListItems.add , , I
            ListView1.ListItems(I).SubItems(1) = a(0)
            ListView1.ListItems(I).SubItems(2) = a(0)
            ListView1.ListItems(I).SubItems(3) = a(0)
            ListView1.ListItems(I).SubItems(4) = a(0)
            ListView1.ListItems(I).SubItems(5) = "1"
'            DoEvents
        Next I
        Close #1
End Sub
现在不出现超界,
只有本站会员才能查看附件,请 登录
图片出来的效果达不到要求,存文件是一行一行的。取文件就乱了,
只有本站会员才能查看附件,请 登录
附文件
在线等
#5
tyms2012-10-14 14:36
刚才那个a全写成a(0),改回测试还是下标超界。
发了附件。。。。
学着编了二个星期。现在刚要有点成果,出了这个问题,心好急
#6
tyms2012-10-14 15:59
哪位版主有空。哪位高手有空。帮看看。
#7
不说也罢2012-10-14 17:34
程序代码:
Private Sub fn_Open_Click()
  Dim I As Long, b As String, a() As String, PP As Integer
    CommonDialog1.ShowOpen             'Show file Open Dialog
    fn = CommonDialog1.FileName        'Get the Chosen File Name
    If fn = "" Then Exit Sub    'Make Sure User have selected a file
         ListView1.ListItems.Clear
       Open fn For Input As #1
       PP = GetCountRow(fn)
       Close #1
         Open fn For Input Access Read Lock Read As #1
         For I = 1 To PP
            ReDim a(3)
            Line Input #1, b
            a = Split(Replace(b, """", ""), ",")
           Dim litem As ListItem
            ListView1.ListItems.add , , I
            ListView1.ListItems(I).SubItems(1) = a(0)
            ListView1.ListItems(I).SubItems(2) = a(1)
            ListView1.ListItems(I).SubItems(3) = a(2)
            ListView1.ListItems(I).SubItems(4) = a(3)
            ListView1.ListItems(I).SubItems(5) = "1"
'            DoEvents
        Next I
        Close #1
End Sub
你再试试吧
#8
tyms2012-10-14 17:50
太谢谢了。总算成了。  原来要去除“”号
1