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

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.