| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2116 人关注过本帖
标题:vb.net 如何在菜单栏显示最近打开的文件?并读取。
只看楼主 加入收藏
fengxye
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2015-9-6
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
vb.net 如何在菜单栏显示最近打开的文件?并读取。
小弟初学编程,搞了一个小程序,想在文件菜单中加一个功能,把最近打开的文件放进去,并能点击打开。不知如何实现。大侠帮忙看一下,如果有源码,麻烦贴一下。
搜索更多相关主题的帖子: 初学编程 菜单栏 如何 
2015-09-08 11:38
SMT86China
Rank: 2
等 级:论坛游民
帖 子:12
专家分:20
注 册:2015-9-8
收藏
得分:0 
以上是VB6的源代码给参考
Option Explicit
Dim A_Name As String
Dim S_Name As String
Const MaxRFiles = 4
Private Sub Command1_Click()
  Unload Me
End Sub

Private Sub Command2_Click()
  ClearRecentFiles
End Sub

Private Sub Command3_Click()
Text1 = GetSetting(A_Name, S_Name, "File0", "")
Text2 = GetSetting(A_Name, S_Name, "File1", "")
Text3 = GetSetting(A_Name, S_Name, "File2", "")
Text4 = GetSetting(A_Name, S_Name, "File3", "")
Text5 = GetSetting(A_Name, S_Name, "FirstFile", "")
End Sub

Private Sub Form_Load()
  A_Name = "Demo"
  S_Name = "RFile"
  ReadRecentFiles
End Sub

Private Sub mExit_Click()
 Unload Me
End Sub

Private Sub mLastFile_Click(Index As Integer)
  UpdateRecentFiles Index
End Sub

Private Sub mOpen_Click()
Dim fIndex As Integer

On Error Resume Next

CommonDialog1.CancelError = True ' Causes a trappable error to occur when the user hits the 'Cancel' button
CommonDialog1.DialogTitle = "打开文件"
CommonDialog1.FileName = ""
CommonDialog1.Filter = "Executables(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNCreatePrompt + cdlOFNHideReadOnly

CommonDialog1.ShowOpen
If Err = cdlCancel Then ' 'Cancel' button was hit
' Add your own code here when the user hits the 'Cancel' button
Else

  fIndex = InRecentFiles(CommonDialog1.FileName)
  If fIndex > MaxRFiles Then
    WriteRecentFiles CommonDialog1.FileName
  Else
    UpdateRecentFiles fIndex
  End If
End If

End Sub
Private Sub WriteRecentFiles(FileName As String)
  Dim fileptr As Integer
  If Len(Trim(FileName)) Then
    fileptr = Val(GetSetting(A_Name, S_Name, "FirstFile", "0"))
    fileptr = IIf(fileptr - 1 >= 0, fileptr - 1, MaxRFiles - 1)
    SaveSetting A_Name, S_Name, "FirstFile", fileptr & ""
    SaveSetting A_Name, S_Name, "File" & fileptr, FileName
    ReadRecentFiles
  End If
End Sub

Private Sub ReadRecentFiles()
    Dim i As Integer
    Dim fileptr As Integer
    Dim rFile As String
    Dim rCount As Integer
    '第一个文件的位置
    fileptr = Val(GetSetting(A_Name, S_Name, "FirstFile", "0"))
    rFile = GetSetting(A_Name, S_Name, "File" & fileptr, "")
    rCount = 0
    Do While Len(rFile) And rCount < MaxRFiles
      mLastFile(rCount).Caption = "&" & (rCount + 1) & " " & rFile
      mLastFile(rCount).Visible = True
      fileptr = IIf(fileptr + 1 < MaxRFiles, fileptr + 1, 0)
      rFile = GetSetting(A_Name, S_Name, "File" & fileptr, "")
      rCount = rCount + 1
    Loop
    If rCount = 0 Then
      mLastFile(rCount).Visible = True
      mLastFile(rCount).Caption = "无历史文件"
      rCount = 1
    End If
    For i = rCount To MaxRFiles - 1
      mLastFile(i).Visible = False
    Next
End Sub

Private Function InRecentFiles(strFile As String) As Integer
    Dim i As Integer
    Dim bFound As Boolean

    'Look for the file specified in strFile
    For i = 0 To MaxRFiles - 1
        If mLastFile(i).Visible And strFile = Mid$(mLastFile(i).Caption, 4) Then
            InRecentFiles = i
            Exit Function
        End If
    Next
    InRecentFiles = MaxRFiles + 1
End Function

Public Sub ClearRecentFiles()
  On Error Resume Next
  Dim i As Integer
  DeleteSetting A_Name, S_Name, "FirstFile"
  For i = 0 To MaxRFiles
    DeleteSetting A_Name, S_Name, "File" & i
  Next
  mLastFile(0).Visible = True
  mLastFile(0).Caption = "无历史文件"
  For i = 1 To MaxRFiles - 1
      mLastFile(i).Visible = False
  Next
End Sub

Public Sub UpdateRecentFiles(fIndex As Integer)
    Dim i As Integer
    Dim fileptr As Integer, FirstPtr As Integer
    Dim FilePtr1 As Integer
    Dim rFile As String, OldFile As String
    Dim rCount As Integer
    If fIndex = 0 Then Exit Sub
    '第一个文件的位置
    FirstPtr = Val(GetSetting(A_Name, S_Name, "FirstFile", "0"))
    'FirstPtr = IIf(FirstPtr - 1 >= 0, FirstPtr - 1, MaxRFiles - 1)
    If fIndex = MaxRFiles - 1 Then
      FirstPtr = IIf(FirstPtr - 1 >= 0, FirstPtr - 1, MaxRFiles - 1)
      SaveSetting A_Name, S_Name, "FirstFile", CStr(FirstPtr)
      ReadRecentFiles
      Exit Sub
    End If
    fileptr = fIndex + FirstPtr
    If fileptr >= MaxRFiles Then fileptr = fileptr - MaxRFiles
    OldFile = GetSetting(A_Name, S_Name, "File" & fileptr, "")
    FilePtr1 = IIf(fileptr - 1 >= 0, fileptr - 1, MaxRFiles - 1)
    'FilePtr1 = IIf(fileptr + 1 < MaxRFiles, fileptr + 1, 0)
    rFile = GetSetting(A_Name, S_Name, "File" & FilePtr1, "")

    Do While FirstPtr <> fileptr And Len(rFile) > 0
      SaveSetting A_Name, S_Name, "File" & fileptr, rFile
      fileptr = FilePtr1
      FilePtr1 = IIf(fileptr - 1 >= 0, fileptr - 1, MaxRFiles - 1)
      'FilePtr1 = IIf(fileptr + 1 < MaxRFiles, fileptr + 1, 0)
      rFile = GetSetting(A_Name, S_Name, "File" & FilePtr1, "")
    Loop
   
    SaveSetting A_Name, S_Name, "File" & FirstPtr, OldFile
    'SaveSetting A_Name, S_Name, "FirstFile", CStr(fileptr)
    ReadRecentFiles
'    WriteRecentFiles OldFile

End Sub
2015-09-08 23:34
fengxye
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2015-9-6
收藏
得分:0 
vb 6 我也有代码,但是转到 就不对了!
2015-09-10 11:05
fengxye
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2015-9-6
收藏
得分:0 
非常感谢SMT86China!!
2015-09-24 21:21
快速回复:vb.net 如何在菜单栏显示最近打开的文件?并读取。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037373 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved