标题:请大大们看看,出了什么问题
只看楼主
tyms
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2012-9-6
结帖率:71.43%
已结贴  问题点数:14 回复次数:7 
请大大们看看,出了什么问题
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)
出现下标越界
请指教,能够给出完成的代码更好

搜索更多相关主题的帖子: file selected 
2012-10-14 11:28
tyms
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2012-9-6
得分:0 
补充 GetCountRow(fn)是取文件多少行函数
2012-10-14 11:30
bczgvip
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:66
帖 子:1310
专家分:5312
注 册:2009-2-26
得分:0 
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
'没测试自己试试吧!
2012-10-14 14:06
tyms
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2012-9-6
得分:0 
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
现在不出现超界,
桌面.zip (9.96 KB)
图片出来的效果达不到要求,存文件是一行一行的。取文件就乱了,
lbw.zip (11.83 KB)
附文件
在线等
2012-10-14 14:23
tyms
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2012-9-6
得分:0 
刚才那个a全写成a(0),改回测试还是下标超界。
发了附件。。。。
学着编了二个星期。现在刚要有点成果,出了这个问题,心好急
2012-10-14 14:36
tyms
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2012-9-6
得分:0 
哪位版主有空。哪位高手有空。帮看看。
2012-10-14 15:59
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:14 
程序代码:
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
你再试试吧

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2012-10-14 17:34
tyms
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2012-9-6
得分:0 
太谢谢了。总算成了。  原来要去除“”号
2012-10-14 17:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-383150-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031673 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved