'文件的读写
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim FSO As Object
Dim FSO_File As Object
Sub SaveFileHistory()
Dim J As Integer
Dim Temp As Boolean
Temp = False '不存在此条记录
'如果不为空,则判断该记录是否已经存在
If Combo1.ListCount <> 0 Then
For J = 0 To Combo1.ListCount - 1
'如果该记录存在,没有必要再循环下去,直接退出for循环
If Combo1.Text = Combo1.List(J) Then
Temp = True ' 存在此条记录
Exit For
End If
Next J
ElseIf Combo1.ListCount = 0 Then
Combo1.AddItem Combo1.Text '如果记录为零则直接添加
End If
If Temp = False Then
Set FSO_File = FSO.OpenTextFile(App.Path + "\Path.txt", ForAppending, True)
FSO_File.WriteLine Combo1.Text
FSO_File.Close
End If
End Sub
'打开文件记录
Private Sub Command2_Click()
CommonDialog1.CancelError = True
On Error GoTo ErrHandler ' 设置标志
CommonDialog1.DialogTitle = "打开JPG文件" '打开对话框的标题
CommonDialog1.FileName = "" '初始化所选文件名
CommonDialog1.Filter = "可执行文件(.JPG)|*.JPG" '文件过滤器,限制打开文件类型
CommonDialog1.ShowOpen '显示打开对话框
Me.Combo1.Text = CommonDialog1.FileName '打开选择的可执行文件
SaveFileHistory ' 保存历史记录
ErrHandler: ' 用户按了"取消"按钮
Exit Sub
End Sub
Private Sub Form_Load()
'读取历史记录到下拉列表
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_File = FSO.OpenTextFile(App.Path + "\Path.txt", ForReading, True)
Do While Not FSO_File.AtEndOfStream
Combo1.AddItem FSO_File.ReadLine
Loop
FSO_File.Close
Combo1.Text = Combo1.List(0)
End Sub
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim FSO As Object
Dim FSO_File As Object
Sub SaveFileHistory()
Dim J As Integer
Dim Temp As Boolean
Temp = False '不存在此条记录
'如果不为空,则判断该记录是否已经存在
If Combo1.ListCount <> 0 Then
For J = 0 To Combo1.ListCount - 1
'如果该记录存在,没有必要再循环下去,直接退出for循环
If Combo1.Text = Combo1.List(J) Then
Temp = True ' 存在此条记录
Exit For
End If
Next J
ElseIf Combo1.ListCount = 0 Then
Combo1.AddItem Combo1.Text '如果记录为零则直接添加
End If
If Temp = False Then
Set FSO_File = FSO.OpenTextFile(App.Path + "\Path.txt", ForAppending, True)
FSO_File.WriteLine Combo1.Text
FSO_File.Close
End If
End Sub
'打开文件记录
Private Sub Command2_Click()
CommonDialog1.CancelError = True
On Error GoTo ErrHandler ' 设置标志
CommonDialog1.DialogTitle = "打开JPG文件" '打开对话框的标题
CommonDialog1.FileName = "" '初始化所选文件名
CommonDialog1.Filter = "可执行文件(.JPG)|*.JPG" '文件过滤器,限制打开文件类型
CommonDialog1.ShowOpen '显示打开对话框
Me.Combo1.Text = CommonDialog1.FileName '打开选择的可执行文件
SaveFileHistory ' 保存历史记录
ErrHandler: ' 用户按了"取消"按钮
Exit Sub
End Sub
Private Sub Form_Load()
'读取历史记录到下拉列表
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_File = FSO.OpenTextFile(App.Path + "\Path.txt", ForReading, True)
Do While Not FSO_File.AtEndOfStream
Combo1.AddItem FSO_File.ReadLine
Loop
FSO_File.Close
Combo1.Text = Combo1.List(0)
End Sub
【VB6 ComboBox 记录打开文件历史记录.zip】点击下载此文件
|
tiancao1001 于 2014-05-13 01:48 PM 发表评论:
使用 Scripting.FileSystemObject 续写文件,不能对历史记录进行清理,记录降越来越多。
发表评论 - 不要忘了输入验证码哦! |