VB问题(帮帮新手啊)

列表框一个(list1) 按钮四个 添加(command1) 删除(command2)
打开(command3) 保存(command4)
文本框四个 text1~text4
目的:1.点击添加按钮将文本框中的值(转换为Integer型)依次添加到列表框中.
2.点击删除按钮将当前选中的条删除,被删除条以后的自动补上被删条的位置.
3.双击列表框中的任意一条,打开form2,并读取出该条的四个值
4.点击保存按钮将列表框中内容保存到文件
5.点击打开命令将保存的文件内容载入到列表框
实在不好意思..遇到的难题有点多..希望各位帮助下小弟(小弟初学VB中...)
妮可
Private Sub Command1_Click() '添加
List1.AddItem Text1 & d & Text2 & d & Text3 & d & Text4
End Sub
每个数据中间有10个空格怎么弄?
如 10[10个空格]20[10个空格]30[10个空格]40
保存的时候通过自己指定文件.

===============补充==========
稍微的动动脑筋呀,我的代码有注释的, 在代码的顶部,有分隔符,如果你想改成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
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-03-27
Private Sub Command1_Click()
List1.Clear
List1.AddItem CInt(Text1.Text)
List1.AddItem CInt(Text2.Text)
List1.AddItem CInt(Text3.Text)
List1.AddItem CInt(Text4.Text)
End Sub

Private Sub Command2_Click()
List1.RemoveItem List1.ListIndex
End Sub

Private Sub Command4_Click()
Open "D:\test\aaa.txt" For Input As #1
List1.Clear
While Not (EOF(1))
Line Input #1, A
List1.AddItem A
Wend
Close #1
End Sub

Private Sub Command3_Click()
Open "D:\test\aaa.txt" For Output As #1
If List1.ListCount <> 0 Then
For i = 0 To List1.ListCount - 1
Print #1, List1.List(i)
Next i
End If
Close #1
End Sub

Private Sub List1_DblClick()
Form2.Show
If List1.ListCount > 0 Then Form2.Label1.Caption = List1.List(0)
If List1.ListCount > 1 Then Form2.Label2.Caption = List1.List(1)
If List1.ListCount > 2 Then Form2.Label3.Caption = List1.List(2)
If List1.ListCount > 3 Then Form2.Label4.Caption = List1.List(3)
End Sub
相似回答