Recent Files - Open in VB.NET

Submitted by: 
Visitors have accessed this post 323 times.

This tutorial will teach you how to open the file and the file location of the recent files in vb.net. This is a continuation of my other tutorial in displaying the recent files.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

2. Next, add only listview named listViewrecentfiles, and two buttons named btnOpenFile for opening the files and btnOpenFileLocation for opening the location of the recent files. Inset ImageList and a ContextStripMenu also. You must design your interface like this:

output

3. Now, we will do the coding. Last time, I put the code of displaying all the recent files in the button. But this time we will put it in the Form_Load.

Import first the following libraries.

  1. Imports System.IO, System.Diagnostics, System.ComponentModel
  1. Dim RecentFolderPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Recent)
  2. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3. listViewrecentfiles.Items.Clear()
  4. Dim lstitem As New ListViewItem()
  5. imageList1.Images.Clear()
  6. For Each RecentFile1 In Directory.GetFiles(RecentFolderPath)
  7. imageList1.Images.Add(RecentFile1, Icon.ExtractAssociatedIcon(RecentFile1))
  8. 'add file and its icon
  9. lstitem = listViewrecentfiles.Items.Add(RecentFile1, RecentFile1)
  10. 'add created date
  11. lstitem.SubItems.Add(New FileInfo(RecentFile1).CreationTime.ToLongDateString())
  12. 'add only name without extension
  13. lstitem.SubItems.Add(Path.GetFileNameWithoutExtension(RecentFile1))
  14. Next
  15. End Sub

For opening the location of the recent files, we will code for the btnOpenFileLocation button and use the Process.Start.

  1. Private Sub btnOpenFileLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFileLocation.Click
  2. Process.Start(RecentFolderPath)
  3. End Sub

For opening the recent files, we will code for the ListView SelectIndexChanged event to view the recent files in the ContextStripMenu.

  1. Private Sub listViewrecentfiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listViewrecentfiles.SelectedIndexChanged
  2. If listViewrecentfiles.SelectedItems.Count > 0 Then
  3. If MouseButtons = MouseButtons.Right Then
  4. contextMenuStrip1.Show(listViewrecentfiles, listViewrecentfiles.SelectedItems(0).Position.X, listViewrecentfiles.SelectedItems(0).Position.Y)
  5. End If
  6. End If
  7. End Sub

And for out button in opening the recent files, we will just use the process.start method with the selected items that was clicked in our listview.

  1. Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
  2. Try
  3. Process.Start(listViewrecentfiles.SelectedItems(0).Text)
  4. Catch m As Win32Exception
  5. End Try
  6. End Sub

Same also with double clicking the items in the listview to open the recent files.

  1. Private Sub listViewrecentfiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listViewrecentfiles.SelectedIndexChanged
  2. If listViewrecentfiles.SelectedItems.Count > 0 Then
  3. If MouseButtons = MouseButtons.Right Then
  4. contextMenuStrip1.Show(listViewrecentfiles, listViewrecentfiles.SelectedItems(0).Position.X, listViewrecentfiles.SelectedItems(0).Position.Y)
  5. End If
  6. End If
  7. End Sub

Output:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.