dcsimg
Results 1 to 21 of 21

Thread: Recent Fie List

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Recent Fie List

    I am working on a program where I want to add a recent file list, much like MS Office.

    I have added a menustrip with a ToolStripMenuItem. I have been able to add a list of recent files to the ToolStripMenuItem. However I am not able to open recent files when clicking on them in the ToolStripMenuItem.

    Doing research I learned that I need to use Add Handler, but have not found a easily understandable explanation on how to do this.

    Is using Add Handler the best and easiest way to do this, if so how?
    Is their a better way to do this, if so how?

    Anybody have a good explanation or know of a site that explains it well.

    Thank You

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    629

    Re: Recent Fie List

    Add Handler is used to create an event handler where one does not already exist. Clicking on a ToolStripMenuItem already raises an event, so just add your file loading code to that. If you double-click on a menu item (in the IDE) it will take you to the event handler for that menu item (just like double-clicking on most other controls) and you can place your file loading code there.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    If you'd like to show us your code for loading your recent file list, i'll show you how to hook up a handler to open your files...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Quote Originally Posted by paulg4ije View Post
    Add Handler is used to create an event handler where one does not already exist. Clicking on a ToolStripMenuItem already raises an event, so just add your file loading code to that. If you double-click on a menu item (in the IDE) it will take you to the event handler for that menu item (just like double-clicking on most other controls) and you can place your file loading code there.
    That's what I was thinking. However while I did not write the code to open the file, I did use a simple message box to test it. Clicking on a recent file under "Recent" did nothing, while clicking on "Recent" displayed the message box. I'm sure I did something wrong but don't know what.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    No one can be sure where your error is for sure without seeing your code.
    Depending on which version of vb you’re using, you can use a lambda sub instead of addressof in your menuitem creation code, but it depends on how you create the recent files list

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Quote Originally Posted by .paul. View Post
    If you'd like to show us your code for loading your recent file list, i'll show you how to hook up a handler to open your files...
    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim DataFilePathString As String = Environ("USERPROFILE")   'Used to get the system drive and file path
            
            'Get the system drive and set the full path to the file containing genre combo box items
            DataFilePathString = Microsoft.VisualBasic.Left(DataFilePathString, 3) & "Users\Public\XMLEditor\"
    
            'Get the list of most recently opened files
            Dim RecentFile As String = DataFilePathString & "Recent.txt"
    
            ReadRecentFile(RecentFile)                                              'Read the Recent.txt file and add contents under File --> Recent
    
        End Sub
    
    
    Private Sub OpenFileDialog_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog.FileOk
            Dim DataFilePathString As String = Environ("USERPROFILE")
    
            DataFilePathString = Microsoft.VisualBasic.Left(DataFilePathString, 3) & "Users\Public\XMLEditor\"
            Dim RecentFile As String = DataFilePathString & "Recent.txt"
    
            WriteRecentFile(RecentFile)                                             'Add the most recently opened file to the Recent.txt file
            ReadRecentFile(RecentFile)                                              'Read the Recent.txt file and add contents under File --> Recent
    
    
        End Sub
    
    Private Sub RecentToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RecentToolStripMenuItem.Click
    
        End Sub
    
        Private Sub WriteRecentFile(ByVal RecentFile As String)
            'Get the system drive and set the full path to the file containing genre combo box items
    
            Try
                Dim recentStreamWriter = New StreamWriter(RecentFile)               'Stream writer to add the most recently opened file to Recent files list (Recent.txt)
    
                recentStreamWriter.WriteLine(pathToXML)                             'Add most recent file to Recent.txt
    
                recentStreamWriter.Close()                                          'Close the file
    
            Catch ex As FileNotFoundException
                If Not File.Exists(RecentFile) Then   'If the Recent.txt file does not exist
                    File.Create(RecentFile)           'Create it
    
                End If
    
            End Try
        End Sub
    
        Private Sub ReadRecentFile(ByVal RecentFile As String)
            'Populate the recent menu item with the contents of file in recent files
            Try
                'Retrieve the file contents to populate Recent file menu strip
                Dim showfilecontents = File.ReadAllLines(RecentFile) 'Retrieve recent files previously used
    
                RecentToolStripMenuItem.DropDownItems.Clear()        'Clear all menu items
    
                If showfilecontents.Count >= 1 Then
                    RecentToolStripMenuItem.Enabled = True
    
                Else
                    Exit Sub
    
                End If
    
                Dim RF As Integer = showfilecontents.Count           'Set the index variable to the number of files used
    
                Do Until RF = showfilecontents.Count - 5 Or RF = 0      'Loop through the contents of the file (showfilecontents variable)
                    RecentToolStripMenuItem.DropDownItems.Add(showfilecontents(RF - 1))   'Add contents of the Recent file to the Recent Menu item
                    RF = RF - 1                                                            'Decrease the index counter by 1
    
                Loop
    
            Catch ex As FileNotFoundException
                If Not File.Exists(RecentFile) Then   'If the Recent file does not exist
                    File.Create(RecentFile)           'Create it
    
                End If
    
            End Try
        End Sub
    The above code is what I have so far.

    I just found an error in my code, not related to the issue I posted about, that is why I did not post it sooner. For some reason when attempting to add a second line to the Recent.txt file. Instead of the file being modified and a line added, the file is being deleted and recreated. According to two books I have I my code should work as intended.

    Does anyone else see why this is happening?

    Thank You

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Code:
    Dim mms As MenuStrip = DirectCast(Me.Controls("ms"), MenuStrip) ' ms being name of menustrip control
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem) ' tsmi being name of Recent ToolStripMenuItem
    Dim di As ToolStripMenuItem = item.DropDownItems.Add("An Item", Nothing, AddressOf clicked)
    di.Tag = "An Item clicked"
    Code:
    Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.tag)
    End Sub

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Are you following a very early .Net book? Any reference to the Microsoft.Visual.Basic namespace is not recommended
    Instead of Left(AString, 3), use AString.SubString(0, 3)

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Here's a simplified example for loading your recent files list, and handling clicked...

    Code:
    Dim mms As MenuStrip = DirectCast(Me.Controls("ms"), MenuStrip) ' ms being name of menustrip control
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem) ' tsmi being name of Recent ToolStripMenuItem
    Dim recentFiles() As String = IO.File.ReadAllLines("Recents.txt")
    For x As Integer = 0 To recentFiles.GetUpperBound(0)
        Dim di As ToolStripMenuItem = item.DropDownItems.Add(IO.Path.GetFileName(recentFiles(x)), Nothing, AddressOf clicked)
        di.Tag = recentFiles(x)
    Next
    Code:
    Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.tag)
    End Sub

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    As for the overwriting issue...

    Code:
    Dim recentStreamWriter As New IO.StreamWriter(recentFiles, True) ' Append = true

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Quote Originally Posted by .paul. View Post
    Code:
    Dim mms As MenuStrip = DirectCast(Me.Controls("ms"), MenuStrip) ' ms being name of menustrip control
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem) ' tsmi being name of Recent ToolStripMenuItem
    Dim di As ToolStripMenuItem = item.DropDownItems.Add("An Item", Nothing, AddressOf clicked)
    di.Tag = "An Item clicked"
    Code:
    Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.tag)
    End Sub
    Thank you for the help.

    Code:
    di.Tag = "An Item clicked"
    Getting a declaration expected error on the above line. Adding a declaration produces an Option Strict requires all declarations to have an As clause.

    Code:
    MsgBox(sender.tag)
    Getting a Option Strict on disallows late binding error on the above line

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Duplicate post
    Last edited by Tesla1886; Feb 2nd, 2020 at 08:15 PM.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Quote Originally Posted by .paul. View Post
    Are you following a very early .Net book? Any reference to the Microsoft.Visual.Basic namespace is not recommended
    Instead of Left(AString, 3), use AString.SubString(0, 3)
    Yes I am using older books. One is copy righted 2002 and the other is for VB 2008. Plus that is the why I learned

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Quote Originally Posted by .paul. View Post
    As for the overwriting issue...

    Code:
    Dim recentStreamWriter As New IO.StreamWriter(recentFiles, True) ' Append = true
    Yes that fixed the issue!

    Thank You.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Quote Originally Posted by Tesla1886 View Post
    Thank you for the help.

    Code:
    di.Tag = "An Item clicked"
    Getting a declaration expected error on the above line. Adding a declaration produces an Option Strict requires all declarations to have an As clause.

    Code:
    MsgBox(sender.tag)
    Getting a Option Strict on disallows late binding error on the above line
    di is the ToolStripMenuItem...

    Code:
    Dim mms As MenuStrip = DirectCast(Me.Controls("ms"), MenuStrip) ' ms being name of menustrip control
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem) ' tsmi being name of Recent ToolStripMenuItem
    Dim di As ToolStripMenuItem = item.DropDownItems.Add("An Item", Nothing, AddressOf clicked)
    di.Tag = "An Item clicked"
    sender is type Object...

    Code:
    Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(DirectCast(sender, ToolStripMenuItem).tag)
    End Sub

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    Quote Originally Posted by .paul. View Post
    di is the ToolStripMenuItem...

    Code:
    Dim mms As MenuStrip = DirectCast(Me.Controls("ms"), MenuStrip) ' ms being name of menustrip control
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem) ' tsmi being name of Recent ToolStripMenuItem
    Dim di As ToolStripMenuItem = item.DropDownItems.Add("An Item", Nothing, AddressOf clicked)
    di.Tag = "An Item clicked"
    sender is type Object...

    Code:
    Private Sub clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(DirectCast(sender, ToolStripMenuItem).tag)
    End Sub
    I moved the line from the declarations section to the Clicked sub

    Now I am getting 'Object reference not set to an instance of an object.' error on the following line.

    Code:
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem)

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Ok seems we have a communication problem. Try using this...

    Code:
    Private Sub GetRecentFiles()
        RecentToolStripMenuItem.DropDownItems.Clear
        Dim recentFiles() As String = IO.File.ReadAllLines("Recents.txt")
        For x As Integer = 0 To recentFiles.GetUpperBound(0)
            Dim di As ToolStripMenuItem = RecentToolStripMenuItem.DropDownItems.Add(IO.Path.GetFileName(recentFiles(x)), Nothing, AddressOf clicked)
            di.Tag = recentFiles(x)
        Next
    End Sub

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Actually that loop should be...

    For x as Integer = 0 to recentFiles.Length - 1

    But the rest is right. Been programming in Java for months. Takes a while to remember vb...

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent Fie List

    I apologize, I just don't understand.

    I copied the following code and pasted it in the declarations section. When running the program I get a declaration expected error as shown in the first image below.

    Code:
    Dim mms As MenuStrip = DirectCast(Me.Controls("ms"), MenuStrip) ' ms being name of menustrip control
    Dim item As ToolStripMenuItem = DirectCast(mms.Items("tsmi"), ToolStripMenuItem) ' tsmi being name of Recent ToolStripMenuItem
    Dim recentFiles() As String = IO.File.ReadAllLines("Recents.txt")
    For x As Integer = 0 To recentFiles.GetUpperBound(0)
        Dim di As ToolStripMenuItem = item.DropDownItems.Add(IO.Path.GetFileName(recentFiles(x)), Nothing, AddressOf clicked)
        di.Tag = recentFiles(x)
    Next
    Using the following code I get an error that says clicked has not been declared as shown in the second image.

    Code:
    Private Sub GetRecentFiles()
        RecentToolStripMenuItem.DropDownItems.Clear
        Dim recentFiles() As String = IO.File.ReadAllLines("Recents.txt")
        For x as Integer = 0 to recentFiles.Length - 1
            Dim di As ToolStripMenuItem = RecentToolStripMenuItem.DropDownItems.Add(IO.Path.GetFileName(recentFiles(x)), Nothing, AddressOf clicked)
            di.Tag = recentFiles(x)
        Next
    End Sub
    If it matters I am using Visual Studio 2019
    Attached Images Attached Images   

  20. #20

    Re: Recent Fie List

    Hello I am having the same problem, I will try to do what they explain here to solve, a hug!

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,941

    Re: Recent Fie List

    Remove the code from the declarations section.
    Add the ReadRecentFile sub and the clicked sub...

    It’s not rocket surgery

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width