Results 1 to 3 of 3

Thread: How to open file from listbox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    10

    How to open file from listbox?

    Hi, im very new in visual basic.
    i have a listbox that displays contents of a folder, and i want to open the selected file to a textbox in form2. how to do this?
    and this is the code i used:

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim folderInfo As New IO.DirectoryInfo("d:\test") 'Or whatever dir path 
            Dim arrFilesInFolder() As IO.FileInfo
            Dim fileInFolder As IO.FileInfo
            arrFilesInFolder = folderInfo.GetFiles("*.*")
            For Each fileInFolder In arrFilesInFolder
                ListBox1.Items.Add(fileInFolder.Name)
            Next
        End Sub
    End Class
    and also how to make the listbox only display the "file name", with "txt" extention file type?
    Thanks before

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: How to open file from listbox?

    Hi and Welcome to the Forum.

    To answer your second question first you would just alter the file pattern you use with the GetFiles Method. Here is another version of the code you have so far:-

    vb.net Code:
    1. Dim myDirInfo As New IO.DirectoryInfo("c:\temp")
    2. ListBox1.Items.AddRange(myDirInfo.GetFiles("*.txt"))

    To then open a file selected in the ListBox in another Form you would then create a Constructor in your second form which can accept a File Name passed from the first Form. This Filename can then be read into a TextBox in the Second Forms Load event i.e:-

    vb.net Code:
    1. Public Class Form2
    2.   Private filenameToOpen As String
    3.  
    4.   'This is the Constructor which accepts a Filename from the Calling Form.
    5.   'This FileName is then Read in the Forms Load Event
    6.   Public Sub New(ByVal currentFileName As String)
    7.     InitializeComponent()
    8.     filenameToOpen = currentFileName
    9.   End Sub
    10.  
    11.   Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    12.     TextBox1.Text = IO.File.ReadAllText(filenameToOpen)
    13.   End Sub
    14. End Class

    Finally, to pass the File Name from the Original Form to the Second Form once a Filename is clicked in the ListBox you would code the LitsBox’s SelectedIndexChanged Event. i.e:-

    vb.net Code:
    1. Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    2.   If ListBox1.SelectedIndex > -1 Then
    3.     Using myFileViewer As New Form2(DirectCast(ListBox1.SelectedItem, IO.FileInfo).FullName)
    4.       myFileViewer.ShowDialog()
    5.     End Using
    6.   End If
    7. End Sub

    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    10

    Re: How to open file from listbox?

    Hi Ian, thanks you for your reply.

    I try to follow your instruction and it works. but how if i want to use a button in form1 to view it? like a view button, so it won't open when i click the name on the listbox.
    And another problem is, the file format is still there on the list box, like.. "note.txt". how to remove the "txt" format so i only get the file name on it?

    Many Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width