dcsimg
 

Creating a Most Recently Used Menu List in .NET

by Hannes DuPreez
Creating a Most Recently Used Menu List in .NET

Learn to keep track of which files were opened and saved so that you can re-open them quicker. Example code is in both C# and VB.NET.

Introduction

Ever wondered how to create an MRU (Most Recently Used) menu list? Well, in today's article I will show you a quick way of keeping track of which files were opened and saved so that you can re-open them quicker.

There are various ways, the Registry being the most popular, but sometimes a PC is set up without giving the user account Registry editing capabilities, or limited, so I have opted to make use of file input and output. This means that the current file name will be written to a certain file, and this file will then be read to get the list of previously opened files.

Let's do a practical example.

Create a new Windows Forms project in either VB.NET or C#, and add a menustrip to it, as shown in Figure 1.

Design
Figure 1: Design

Let's add some code!

Add the System.IO namespace because we will be working with files.

C#

using System.IO;

VB.NET

Imports System.IO

Create a Queue object to store the MRU items when read.

C#

   private Queue<string> MRUlist = new Queue<string>();

VB.NET

   Dim MRUlist As Queue(Of String) = New Queue(Of String)()

The Queue of string objects will hold all the items present in the current MRU list. To get the items, the items first need to be saved into a txt file and later read from that same file. Let's add the logic to save the recent items into a file now.

C#

   private void SaveRecentFile(string strPath)
   {
      RecentToolStripMenuItem.DropDownItems.Clear();

      LoadRecentList();

      if (!(MRUlist.Contains(strPath)))

         MRUlist.Enqueue(strPath);

      while (MRUlist.Count > 5)

         MRUlist.Dequeue();

      foreach (string strItem in MRUlist)
      {
         ToolStripMenuItem tsRecent = new
            ToolStripMenuItem(strItem, null);

         RecentToolStripMenuItem.DropDownItems.Add(tsRecent);
      }

      StreamWriter stringToWrite = new
         StreamWriter(System.Environment.CurrentDirectory +
         @"\Recent.txt");

      foreach (string item in MRUlist)

         stringToWrite.WriteLine(item);

      stringToWrite.Flush();

      stringToWrite.Close();
   }

VB.NET

   Private Sub SaveRecentFile(strPath As String)

      RecentToolStripMenuItem.DropDownItems.Clear()

      LoadRecentList()

      If Not (MRUlist.Contains(strPath)) Then

         MRUlist.Enqueue(strPath)

      End If

      While MRUlist.Count > 5

         MRUlist.Dequeue()

      End While

      For Each strItem As String In MRUlist

         Dim tsRecent As New ToolStripMenuItem(strItem, Nothing)

         RecentToolStripMenuItem.DropDownItems.Add(tsRecent)

      Next

      Dim stringToWrite As New StreamWriter(System.Environment _
         .CurrentDirectory + "\Recent.txt")

      For Each item As String In MRUlist

         stringToWrite.WriteLine(item)

      Next

      stringToWrite.Flush()

      stringToWrite.Close()

   End Sub

Now, let's add the loading of the file names from the file.

C#

   private void LoadRecentList()
   {
      MRUlist.Clear();

      try
      {
         StreamReader srStream = new StreamReader
            (Environment.CurrentDirectory + @"\Recent.txt");

         string strLine = "";

         while ((InlineAssignHelper(ref strLine,
               srStream.ReadLine())) != null)

            MRUlist.Enqueue(strLine);

         srStream.Close();
      }
      catch (Exception ex)
      {
         MessageBox.Show("Error");
      }
   }

  private static T InlineAssignHelper<T>(ref T target, T value)
   {
      target = value;
      return value;
   }

VB.NET

   Private Sub LoadRecentList()

      MRUlist.Clear()

      Try

         Dim srStream As New StreamReader _
            (System.Environment.CurrentDirectory + _
               "\Recent.txt")

         Dim strLine As String

         While (InlineAssignHelper(strLine, srStream.ReadLine())) _
               IsNot Nothing

            MRUlist.Enqueue(strLine)

         End While

         srStream.Close()

      Catch ex As Exception

         MessageBox.Show("Error")

      End Try
   End Sub

   Private Shared Function InlineAssignHelper(Of T) _
         (ByRef target As T, ByVal value As T) As T

      target = value
      Return value

   End Function

Finally, let's add the Form_Load event.

C#

   private void Form1_Load_1(object sender, EventArgs e)
   {
      LoadRecentList();

      foreach (string item in MRUlist)
      {
         ToolStripMenuItem fileRecent = new ToolStripMenuItem(item,
            null, RecentFile_click());

         RecentToolStripMenuItem.DropDownItems.Add(fileRecent);
      }
   }

VB.NET

   Private Sub Form1_Load(sender As Object, e As EventArgs) _
         Handles Me.Load
      LoadRecentList()

      For Each item As String In MRUlist

         Dim fileRecent As New ToolStripMenuItem(item, Nothing,

            RecentFile_click())

         RecentToolStripMenuItem.DropDownItems.Add(fileRecent)

      Next
   End Sub

Conclusion

Having a recent menu list is a nice little feature to add to your apps and it makes them look more polished.

This article was originally published on Thursday Apr 23rd 2020
Home
Mobile Site | Full Site