主窗体:
Public Sub lsvload() '初始化listview控件的列表头
lsv.ColumnHeaders.Add , , , 0
lsv.ColumnHeaders.Add , , "网站名称", 1500
lsv.ColumnHeaders.Add , , "网站地址(双击记录可打开网站)", 4000
lsv.ColumnHeaders.Add , , "网站类型", 1500
lsv.ColumnHeaders.Add , , "登陆名", 1300
lsv.ColumnHeaders.Add , , "登陆密码", 1300
End Sub
Public Sub output() '读取文件中的信息,并将它们显示在listview上
On Error Resume Next
Dim str As String
Dim lsv1 As ListItem
Dim i As Integer
Open App.Path & "/data.txt" For Input As #1
While Not EOF(1)
For i = 1 To 5 '一次读5行
Line Input #1, str
If i = 1 Then '这表示是该记录的第一个元素
Set lsv1 = lsv.ListItems.Add
lsv1.SubItems(i) = str
Else
lsv1.SubItems(i) = str
End If
Next
Wend
Close #1
End Sub
Private Sub cmdadd_Click()
frmadd.Show
End Sub
Private Sub cmddelete_Click()
If MsgBox("是否真的要删除以下网址:" & vbCrLf & lsv.SelectedItem.SubItems(2), vbYesNo, "确认删除") = vbYes Then
lsv.ListItems.Remove (lsv.SelectedItem.Index)
Call inputn
End If
End Sub
Private Sub cmdmodify_Click()
If lsv.ListItems.Count = 0 Then
Exit Sub
End If
frmadd.Show '把列表中的数据项放到修改窗口,以便修改
frmadd.Caption = "修改所选"
frmadd.Text2.Enabled = False
frmadd.Text2.BackColor = &HC0E0FF
frmadd.Command1.Caption = "修改"
frmadd.Text2.Text = lsv.SelectedItem.SubItems(2)
frmadd.Text1.Text = lsv.SelectedItem.SubItems(1)
frmadd.Text3.Text = lsv.SelectedItem.SubItems(3)
frmadd.Text4.Text = lsv.SelectedItem.SubItems(4)
frmadd.Text5.Text = lsv.SelectedItem.SubItems(5)
End Sub
Private Sub Command1_Click()
On Error GoTo quxiao
CommonDialog1.Flags = &H2 '提示是否覆盖
CommonDialog1.ShowSave
FileCopy App.Path & "/data.txt", CommonDialog1.FileName
Exit Sub
quxiao: '用户选择“取消”后产生错误,捕获此错误可以避免取消后生成垃圾文件
Exit Sub
End Sub
Private Sub Form_Load()
If App.PrevInstance = True Then '检查是否已经打开程序
Unload frmmain
Exit Sub
End If
On Error Resume Next
Call jiancha
Call lsvload
Call output
lsv.SelectedItem.Selected = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload frmadd
End Sub
Private Sub lsv_DblClick()
Dim str As String
Dim xx
str = lsv.SelectedItem.SubItems(2)
xx = Shell("C:/Program Files/Internet Explorer/IEXPLORE.EXE " & str, 3)
End Sub
frmadd窗体:
Dim ctl As Control '控制输入必要信息
For Each ctl In Controls
If TypeOf ctl Is TextBox Then
If Text1.Text = "" Then
Text1.SetFocus
Exit Sub
End If
If Text2.Text = "" Then
Text2.SetFocus
Exit Sub
End If
If Text3.Text = "" Then
Text3.SetFocus
Exit Sub
End If
End If
Next ctl
If Command1.Caption = "增加" Then
Dim lsv1 As ListItem
Set lsv1 = frmmain.lsv.ListItems.Add()
lsv1.SubItems(1) = Text1
lsv1.SubItems(2) = Text2
lsv1.SubItems(3) = Text3
lsv1.SubItems(4) = Text4
lsv1.SubItems(5) = Text5
MsgBox "成功增加", vbInformation, "恭喜"
Else
frmmain.lsv.SelectedItem.SubItems(1) = Text1
frmmain.lsv.SelectedItem.SubItems(2) = Text2
frmmain.lsv.SelectedItem.SubItems(3) = Text3
frmmain.lsv.SelectedItem.SubItems(4) = Text4
frmmain.lsv.SelectedItem.SubItems(5) = Text5
End If
Unload Me
Call inputn
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
End Sub
模块代码:
Public Sub inputn()
On Error Resume Next
Dim i As Integer
Dim j As Integer
For i = 0 To frmmain.lsv.ListItems.Count
For j = 1 To 5
Open App.Path & "/data.txt" For Output As #2
Print #2, frmmain.lsv.ListItems.Item(i).SubItems(j)
Next
Next
Close #2
End Sub
-------------------------
Public Sub jiancha() '运行程序时检查.是否已经打开?是否有数据文件
Dim fso
On Error GoTo chuli '检查是否有data.bat这个文件,要是没有则创建.适合第一次运行此程序.
Open App.Path & "/data.txt" For Input As #1
Close #1
Exit Sub
chuli:
Set fso = CreateObject("scripting.filesystemobject")
fso.CreateTextFile (App.Path & "/data.txt") '创建这个文件
End Sub