===============补充==========
稍微的动动脑筋呀,我的代码有注释的, 在代码的顶部,有分隔符,如果你想改成10个空格,你可以改成d=10个空格呀
代码顶部也有那个变量存放着路径呀.. 我是固定的,如果你想随时修改,可以改成:
dim saveFile$
然后新建一个文本框,用来输入路径,比如叫txtpath,这个文本框的change事件中,使用:
saveFile=trim(txtpath)
==============================
举个实例吧, 有几条,我没太看懂..
比如说:
text1=3 text2=4 text3=8 text4=99
点一下添加: list1里面添加4条记录??
分别是
3
4
8
99
那你说的第3条,双击列表中的一条.. 读取此条的4个值??
如果用1条来记录4个值, 那为什么还要转为int型呀? 那就有分隔符来保存4条了,就不在是int型,而是字符型了, 比如:
list1的第1条为:
3,4,8,99
其它都很容易..
================代码来了==================
Const d$ = "," '数据分隔符
Const saveFile$ = "c:\list1.dat" '存储路径
Private Sub Command1_Click() '添加
List1.AddItem Text1 & d & Text2 & d & Text3 & d & Text4
End Sub
Private Sub Command2_Click() '删除
If List1.ListIndex > -1 Then List1.RemoveItem List1.ListIndex
End Sub
Private Sub Command3_Click() '打开
Dim lineStr$
If Len(Dir(saveFile)) = 0 Then Exit Sub '找不到文件时
Open saveFile For Input As #1 ' 打开文件。
Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, lineStr$ ' 读入一行数据并将其赋予某变量。
If UBound(Split(Trim(lineStr), d)) = 3 Then
List1.AddItem Trim(lineStr)
End If
Loop
Close #1 ' 关闭文件。
End Sub
Private Sub Command4_Click() '保存
Dim i&
'如果每次保存为追加,请改output为Append
Open saveFile For Output As #1 ' 打开文件。
For i = 0 To List1.ListCount - 1
Print #1, List1.List(i)
Next
Close #1 ' 关闭文件。
End Sub
Private Sub List1_DblClick() '双击读取数据到文本框
Dim tmp$()
tmp = Split(List1.Text, d)
If UBound(tmp) = 3 Then '如果数据正确
Text1 = tmp(0)
Text2 = tmp(1)
Text3 = tmp(2)
Text4 = tmp(3)
End If
End Sub
温馨提示:答案为网友推荐,仅供参考