Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Add to recent - Automatically create recent items

0.00/5 (No votes)
20 Oct 2007 1  
Automatically create recent menu items, it's very easy and useful. You just need to define file to save, and menu button (class will automatically create all items).

Screenshot - vb_recent.jpg

Introduction

This class automatically creates recent menu items. This class is very easy to use and is very useful. You only need to create a menu button (with index 0 to define a control array), define a file for saving recent items, and when you want, call the AddItem method. The class will automatically create all the menu buttons, delete the old buttons, set the button captions with item names, and button tag properties with item data values. You can also set the maximum count of recent items, and when you add items that already exist, the class will delete the old item and set the new items at the start (so it will be first on the list).

How to use

It is very easy to use this class. You just need to add this class to your project, define the maximum number of recent items (default is 5), create a menu button (with index 0 to define the control array), and that is all.

Functions / subs

  • AddItem(ByVal str_name As String, ByVal str_data As String) – insert new recent item with name str_name and value str_data
  • Data(ByVal Index As Integer) As String – return recent item data (value)
  • Name(ByVal Index As Integer) As String – return recent item name
  • LoadFile(ByVal str_file As String) As Boolean – load recent items from file; if failed, return false
  • RemoveItem(ByVal Index As Integer) – delete recent item
  • SaveData(ByVal str_file As String) – save recent items to file
  • SetMenu(ByRef objMenu As Object) – create menu items

Example (code for using recent items)

'show tag (data of recent item), button caption is
' recent item name
Private Sub buttRecent_Click(Index As Integer)
    MsgBox Me.buttRecent(Index).Tag
End Sub

'add new item to recent
Private Sub buttSave_Click()
    recent.AddItem Me.txtName.Text, Me.txtVal.Text
    recent.setMenu Me.buttRecent
End Sub

'load item on form load
Private Sub Form_Load()
    recent.MaxCount = 5 'number of recent items to display
    recent.LoadFile App.Path & "\rec.txt"
    'add items to control array buttRecent,
    ' class will do this (remove all items,and add new)
    recent.setMenu Me.buttRecent
End Sub

'save items to file on form terminate
Private Sub Form_Terminate()
    recent.SaveData App.Path & "\rec.txt"
    Set recent = Nothing
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here