1,105,197 Community Members

load items into combobox from file names in a specified directory

Member Avatar
boywonda1
Newbie Poster
2 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hey all..
I'm new to VB.net and i wanted to find out how i could load the names of files(in a specified dir) into a combobox?

I knw i should have posted code..but i only have an idea as to what should happen.
correct me where i'm wrong.

1. get the number of files in specified dir
2. add names of files as items in combobox (using a loop structure)
3. when an item is selected. display the contents in a textbox

my idea in pseudocode:

for counter = 0 to numberOfFiles
combobox.add(fileName)
next
if (combobox.selectedItem = fileName)
open File
display Contents in Textbox
end if


thnx in advance.
Zikani

Member Avatar
boywonda1
Newbie Poster
2 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i worked on it...works ok so far but i know theres a better way to work on it...

Public Class Form1
    Dim strFileName(5) As String
    Dim strPath As String

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDefnTerms.SelectedIndexChanged
        strPath = "C:\Definition Data"
        txtDefinition.Text = My.Computer.FileSystem.ReadAllText(strPath & strFileName(cmbDefnTerms.SelectedIndex))

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strPath = "C:\Definition Data\"
        Dim i As Integer
        Dim strFile(5) As String
        For i = 0 To 3
            strFile(i) = My.Computer.FileSystem.GetFiles(strPath).Item(i)
            strFileName(i) = strFile(i).Remove(0, strFile(i).LastIndexOf("\"))
            cmbDefnTerms.Items.Add(strFileName(i))
        Next
        

    End Sub

End Class

what do you think?

Member Avatar
lolafuertes
Posting Shark
912 posts since Oct 2008
Reputation Points: 145 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 7 [?]
 
0
 

If it works do not change it, always says my teacher. ;)

Maybe I can suggest another way to get that, but only for discussion. This has not been tested:

Public Class Form1
'
' Do you really want to have only 5 files?
' I will suggest to work with FileInfo instead, because you can have more control on what is happening with the file
'
'    Dim strFile() As FileInfo  ' But really we need no array
    Const strPath As String = "C:\Definition Data" ' declaring as a constant will not waste time nor memory on each new assignement

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'         strPath = "C:\Definition Data\"
'        Dim i As Integer
'        Dim strFile(5) As String
'        For i = 0 To 3
'    I will suggest to change this in a for each loop 
         For each existingFile as FileInfo in My.Computer.FileSystem.GetFiles(strPath)
'            strFileName(i) = strFile(i).Remove(0, strFile(i).LastIndexOf("\"))
'            cmbDefnTerms.Items.Add(strFileName(i))
' add it directly as object in the combo. 
' The file names will be OK as the default ToString for a FileInfo
' obtained using GetFiles is the file name, not the full path
              cmbDefnTerms.Items.Add(existingFile)
        Next
        
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDefnTerms.SelectedIndexChanged
'         strPath = "C:\Definition Data"
        Dim FiletoShow as FileInfo = Ctype(cmbDefnTerms.SelectedItem, FileInfo) ' this will retrieve the selecte Item 
        If Filetoshow.Exists then ' you will verify that the file exists before showing it
            try
                 txtDefinition.Text = My.Computer.FileSystem.ReadAllText(FiletoShow.FullName) ' here will use the full path 
            catch e as Exception
                 ' in case of error clear the textDefinfition
                 txtDefinition.Text = ""
            End try
        End If
    End Sub


End Class

See see here for the explanation on how works the ToString for the FileInfo class.

Well, this is only anoter approach.

Hope this helps

Question Answered as of 2 Years Ago by lolafuertes
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: