Welcome to Dream.In.Code
Become a VB Expert!

Join 137,228 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,898 people online right now. Registration is fast and FREE... Join Now!




Open Recent Files

 
Reply to this topicStart new topic

Open Recent Files

brottmayer
7 Feb, 2007 - 10:46 PM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
Just wondering if there was an easier way of accomplishing what this site: MRU Menu, is providing? To be able to have a open recent section within my program, much like that of Adobe Illustrator and Photoshop and the likes. Any suggestions would be great. Thanks.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Open Recent Files
8 Feb, 2007 - 04:43 AM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Search the Registry for Keys with "MRU" in it and look inside them for the most recently used files.

Alternatively, you can save the filename into a separate file everytime your application works on one.
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Open Recent Files
9 Feb, 2007 - 05:01 PM
Post #3

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
Can someone point me in the right direction with this functionality? I found this code, online, but it's confusing and is not accomplishing what I want to achieve, which is to store a list of the most recently opened files.

CODE

Option Explicit

Private Const MaxMRU = 4  'Maximum number of MRUs in list (-1 for no limit)
Private Const NotFound = -1   'Indicates a duplicate entry was not found
Private Const NoMRUs = -1     'Indicates no MRUs are currently defined

Private MRUCount As Long      'Maintains a count of MRUs defined

Private Sub Form_Load()
   ' Initialize the count of MRUs
   MRUCount = NoMRUs
  
   ' Call sub to retrieve the MRU filenames
   GetMRUFileList
End Sub

Private Sub Form_Unload(Cancel As Integer)
   ' Call sub to save the MRU filenames
   SaveMRUFileList
End Sub

Private Sub mnuMRU_Click(Index As Integer)
   ' Call sub to reorder the MRU list
   ReorderMRUList mnuMRU(Index).Caption, CLng(Index)
End Sub

Private Sub mnuOpen_Click()
   ' Show the file open common dialog
   Me.CommonDialog1.ShowOpen
  
   ' Call sub to add this file as an MRU
   AddMRUItem Me.CommonDialog1.FileName
End Sub

Private Sub AddMRUItem(NewItem As String)
   Dim result As Long
      
   ' Call sub to check for duplicates
   result = CheckForDuplicateMRU(NewItem)
  
   ' Handle case if duplicate found
   If result <> NotFound Then
      ' Call sub to reorder MRU list
      ReorderMRUList NewItem, result
   Else
      ' Call sub to add new item to MRU menu
      AddMenuElement NewItem
   End If
End Sub

Private Function CheckForDuplicateMRU(ByVal NewItem As String) As Long
   Dim i As Long
  
   ' Uppercase newitem for string comparisons
   NewItem = UCase$(NewItem)
  
   ' Check all existing MRUs for duplicate
   For i = 0 To MRUCount
      If UCase$(Me.mnuMRU(i).Caption) = NewItem Then
         ' Duplicate found, return the location of the duplicate
         CheckForDuplicateMRU = i
        
         ' Stop searching
         Exit Function
      End If
   Next i
  
   ' No duplicate found, so return -1
   CheckForDuplicateMRU = -1
End Function

Private Sub mnuQuit_Click()
   ' Close the program
   Unload Me
End Sub

Private Sub AddMenuElement(NewItem As String)
   Dim i As Long
  
   ' Check that we will not exceed maximum MRUs
   If (MRUCount < (MaxMRU - 1)) Or (MaxMRU = -1) Then
      'Increment the menu count
      MRUCount = MRUCount + 1
      
      ' Check if this is the first item
      If MRUCount <> 0 Then
         ' Add a new element to the menu
         Load mnuMRU(MRUCount)
      End If
      
      ' Make new element visible
      mnuMRU(MRUCount).Visible = True
   End If
  
   ' Shift items to maintain most recent to least recent
   For i = (MRUCount) To 1 Step -1
      ' Set the captions
      mnuMRU(i).Caption = mnuMRU(i - 1).Caption
   Next i
  
   ' Set caption for new item
   mnuMRU(0).Caption = NewItem
End Sub

Private Sub ReorderMRUList(DuplicateMRU As String, DuplicateLocation As Long)
   Dim i As Long
  
   ' Move entries previously "more recent" than the
   ' duplicate down one in the MRU list
   For i = DuplicateLocation To 1 Step -1
      mnuMRU(i).Caption = mnuMRU(i - 1).Caption
   Next i
  
   ' Set caption of newitem
   mnuMRU(0).Caption = DuplicateMRU
End Sub

Private Sub GetMRUFileList()
   Dim i As Long           'Loop control variable
   Dim result As String    'Name of MRU from registry
  
   ' Loop through all entries
   Do
      ' Retrieve entry from registry
      result = GetSetting(App.Title, "MRUFiles", Trim$(CStr(i)), "")
      
      ' Check if a value was returned
      If result <> "" Then
         ' Call sub to additem to MRU list
         AddMRUItem result
      End If
      
      ' Increment counter
      i = i + 1
   Loop Until (result = "")
End Sub

Private Sub SaveMRUFileList()
   Dim i As Long           ' Loop control variable
  
   ' Loop through all MRU
   For i = 0 To MRUCount
      ' Write MRU to registry with key as it's position in list
      SaveSetting App.Title, "MRUFiles", Trim$(CStr(i)), mnuMRU(i).Caption
   Next i
End Sub

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Open Recent Files
9 Feb, 2007 - 08:36 PM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Actually, this wont work because it assumes that you have stored the MRU File paths using the SaveSetting Function.

The SaveSetting Function stores data in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\YourAppName

You can try searching through files in the C:\Documents and Settings\<user>\Recent directory.

User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Open Recent Files
9 Feb, 2007 - 08:44 PM
Post #5

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(born2c0de @ 9 Feb, 2007 - 09:36 PM) *

Actually, this wont work because it assumes that you have stored the MRU File paths using the SaveSetting Function.

The SaveSetting Function stores data in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\YourAppName

You can try searching through files in the C:\Documents and Settings\<user>\Recent directory.



Please forgive me for my lack of knowledge, I am learning and would like to learn. Could you explain to me the steps that I would take to accomplish this and to make sure it works as well? Thanks.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Open Recent Files
9 Feb, 2007 - 11:32 PM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Relax, everyone on earth lacks knowledge in something or the other.

First I need to know what exactly you need.
When you say MRU Files, are you concerned with only a certain set of files (for example .dat or .mp3 files) or just any files that were used by the user?

Which type of files does your application deal with?
You can just store the name of each file (as soon as it is used by your application) into another file, and then Open it later and display these Filenames in your Menu Dynamically.

Post your needs and requirements as it will be easier for us to help you.

Once we get that, we can help you out with code too.
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Open Recent Files
10 Feb, 2007 - 06:46 AM
Post #7

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(born2c0de @ 10 Feb, 2007 - 12:32 AM) *

Relax, everyone on earth lacks knowledge in something or the other.

First I need to know what exactly you need.
When you say MRU Files, are you concerned with only a certain set of files (for example .dat or .mp3 files) or just any files that were used by the user?

Which type of files does your application deal with?
You can just store the name of each file (as soon as it is used by your application) into another file, and then Open it later and display these Filenames in your Menu Dynamically.

Post your needs and requirements as it will be easier for us to help you.

Once we get that, we can help you out with code too.



Well basically I am building a text editor (start-up program) that can read rtf's, txt's, and etc. Those are the type of files that I would like to have shown in the recently opened files. Not necessarily going with the MRU, if there is a better way then i am all ears. That was the only reference that I saw on the net about it. Thanks for helping me born2c0de.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Open Recent Files
12 Feb, 2007 - 05:49 AM
Post #8

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Ok Great.
What you need to do is to store the filename of every file that your program has used.

Like for example, as soon as the user clicks "Open", once the file is selected, save the filename either in the registry or in a separate file (say mru.txt)

Then open the registry key/file and check for filename entries. Add them dynamically to your "File" Menu.
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Open Recent Files
12 Feb, 2007 - 07:35 AM
Post #9

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(born2c0de @ 12 Feb, 2007 - 06:49 AM) *

Ok Great.
What you need to do is to store the filename of every file that your program has used.

Like for example, as soon as the user clicks "Open", once the file is selected, save the filename either in the registry or in a separate file (say mru.txt)

Then open the registry key/file and check for filename entries. Add them dynamically to your "File" Menu.



OK, I understand that. But how do I create a separate file that is used to store this information and on the flipside, to pull the information from this file into the program? Thanks.
User is offlineProfile CardPM
+Quote Post

Ryan747
RE: Open Recent Files
12 Feb, 2007 - 05:51 PM
Post #10

New D.I.C Head
*

Joined: 16 Oct, 2006
Posts: 26


My Contributions
to create a separate file you need to create a text file to do this you
need to create this code:

CODE

    Open "c:\mru.txt" For Output As #1
    Print #1, 'Path and filename of recently opened files
    Close #1


all you have to do now is to retrieve the content of the mru.txt then you now have a recent opened files...
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Open Recent Files
13 Feb, 2007 - 08:30 AM
Post #11

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE(Ryan747 @ 13 Feb, 2007 - 07:21 AM) *

to create a separate file you need to create a text file to do this you
need to create this code:

CODE

    Open "c:\mru.txt" For Output As #1
    Print #1, 'Path and filename of recently opened files
    Close #1


all you have to do now is to retrieve the content of the mru.txt then you now have a recent opened files...


Yes, but in your case you should use Append instead of Output like this:
CODE

open "c:\mru.txt" for append as #1


Using Append Mode will add data to a file. Using output erases the original data and puts in the new.

File Handling is tricky, I couldn't find any decent tutorials on it immediately, so since I'm already writing one on it, I'll post it in a few days or so.

If you have any problems writing the code, let us know. smile.gif
User is offlineProfile CardPM
+Quote Post

brottmayer
RE: Open Recent Files
13 Feb, 2007 - 09:30 PM
Post #12

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 67


My Contributions
QUOTE(born2c0de @ 13 Feb, 2007 - 09:30 AM) *

Yes, but in your case you should use Append instead of Output like this:
CODE

open "c:\mru.txt" for append as #1


Using Append Mode will add data to a file. Using output erases the original data and puts in the new.

File Handling is tricky, I couldn't find any decent tutorials on it immediately, so since I'm already writing one on it, I'll post it in a few days or so.

If you have any problems writing the code, let us know. smile.gif


Yeah, I think I might know how to do it, but as you saw in a previous question on this forum, I had asked about saving a file and overwriting it. So if you could explain further, that would be great. Thanks.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 03:06PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month