如何在vb中用动态菜单显示历史记录
1个回答
2015-06-29
展开全部
'收集自网络汐木,供参考
Dim FileCout As Integer
Dim RFileList() As String
Private Sub Form_Load()
GetFileCout '获取recentfile.dat的项目条数
If FileCout <> 0 Then '如果项目条数不为0,则创建历史记录菜单
CreatMenu
Else
recentfile(0).Caption = "无历史记录"
End If
End Sub
Private Sub GetFileCout_r() '用于获取recentfile.dat中的记录个数
Dim TempString As String
FileCout = 0
Open App.Path & "\recentfile.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, TempString
If TempString <> "" Then
FileCout = FileCout + 1
End If
Loop
Close #1
ReDim RFileList(FileCout) '将recentfile.dat中的记录写入数组RFileList
FileCout = 0
Open App.Path & "\recentfile.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, TempString
If TempString <> "" Then
RFileList(FileCout) = TempString
FileCout = FileCout + 1
End If
Loop
Close #1
End Sub
Private Sub CreatMenu() '动态创建菜单项
Dim i As Integer
For i = 1 To recentfile.Count - 1
Unload recentfile(i)
Next
recentfile(0).Caption = "清除历史记录"
For i = 1 To UBound(RFileList)
Load recentfile(i)
recentfile(i).Caption = RFileList(i - 1)
Next
End Sub
Function WriteFRecord(ByVal Path As String) '将打开过的文件路径写入recentfile.dat
Dim i As Integer
GetFileCout
If FileCout < 5 Then
Open App.Path & "\recentfile.dat" For Append As #1
Print #1, vbCrLf & Path
Close #1
Else
RFileList(0) = RFileList(1)
RFileList(1) = RFileList(2)
RFileList(2) = RFileList(3)
RFileList(3) = RFileList(4)
RFileList(4) = Path
Open App.Path & "\recentfile.dat" For Output As #1
For i = 0 To UBound(RFileList) - 1
Print #1, RFileList(i)
Next
Close #1
End If
End Function
Private Sub openfile_Click() '打开文件
Dim SameFile As Boolean
Dim i As Integer
CommonDialog1.ShowOpen
If UBound(RFileList) <> 0 Then
For i = 0 To UBound(RFileList) - 1
If RFileList(i) = CommonDialog1.FileName Then SameFile = True
Next
If SameFile = False Then WriteFRecord (CommonDialog1.FileName)
Else
WriteFRecord (CommonDialog1.FileName)
End If
GetFileCout
If FileCout <> 0 Then
CreatMenu
Else
recentfile(0).Caption = "无历史记录"
End If
End Sub
Private Sub recentfile_Click(Index As Integer) '单击菜单选项时的响应 汐木
Dim Result As Integer
Dim i As Integer
If Index <> 0 Then
MsgBox ("打开文件" & recentfile(Index).Caption)
Else
If recentfile(0).Caption = "清除历史记录" Then
Result = MsgBox("需要清除历史记录吗?", vbYesNo, "提示")
If Result = 6 Then
For i = 1 To recentfile.Count - 1
Unload recentfile(i)
recentfile(0).Caption = "无记录"
Next
Open App.Path & "\recentfile.dat" For Output As #1
Close #1
End If
End If
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询