Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List of Files in ComboBox

Status
Not open for further replies.

kevinnaomi

IS-IT--Management
Sep 26, 2002
81
0
0
US
Hi,
I am trying to obtain a list of files in a folder, and have the populate to a combo box.
I have no idea how I should do this.

Can anyone give me a suggestion.

e.g. I want to list the files in c:\temp and be able to select one in a combo box..thus opening the file.
 
You can do something like this...
Code:
Dim files() As String
files = System.IO.Directory.GetFiles("c:\temp")
For Each f As String In files
ComboBox1.Items.Add(System.IO.Path.GetFileName(f))
Next
you can use collection for this...
Code:
Dim files() As String
Dim fileEnum As System.Collections.IEnumerator
files = System.IO.Directory.GetFiles("c:\temp")
fileEnum = files.GetEnumerator()
While fileEnum.MoveNext
ComboBox1.Items.Add(Convert.ToString(fileEnum.Current))
End While
/code]
Then according to the file type you can have the code to open that file.

Sharing the best from my side...

--Prashant--
 
PGoRule,
Thank you for this code.

I am sorry, I am not sure where to copy and paste this code.

I tried to put in within the combobox code:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim files() As String
Dim fileEnum As System.Collections.IEnumerator
files = System.IO.Directory.GetFiles("c:\temp")
fileEnum = files.GetEnumerator()
While fileEnum.MoveNext
ComboBox1.Items.Add(Convert.ToString(fileEnum.Current))
End While
End Sub

But the files in the folder didn`t show.
 
' Add the following controls:
' ComboBox1
' Button1
' FolderBrowserDialog1

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dir_path As String = "", file_name As String = ""

FolderBrowserDialog1.SelectedPath = ""
ComboBox1.Items.Clear()

Do
FolderBrowserDialog1.ShowDialog()
dir_path = FolderBrowserDialog1.SelectedPath & "\"
Loop While FolderBrowserDialog1.SelectedPath = ""

file_name = Dir(dir_path, FileAttribute.Normal)

Do While file_name <> ""
ComboBox1.Items.Add(file_name)
file_name = Dir()
Loop
End Sub
End Class
 
Shorter way.

Dim files() As String = System.IO.Directory.GetFiles("c:\temp")
Me.ComboBox1.Items.Addrange(files)
 
TipGiver,
Thanks. But where should this go in the code?
 
The code should be in a button click event handler.
e.g

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' code here
end sub

The code where you put it before, will be fired as you change the index (select an other item) of the combo. By default, the combo is empty so the index cannot be changed
 
TipGiver,
I tried this, but it doesn`t show the files that are in the temp folder. My code looks like this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim files() As String = System.IO.Directory.GetFiles("c:\temp")
Me.ComboBox1.Items.AddRange(files)
End Sub
 
Simply add a combobox to your form.
In form load event, just add the code given in above posts.
Something like this...
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim files() As String
Dim fileEnum As System.Collections.IEnumerator
files = System.IO.Directory.GetFiles("c:\temp")
fileEnum = files.GetEnumerator()
While fileEnum.MoveNext
ComboBox1.Items.Add(Convert.ToString(fileEnum.Current))
End While
End Sub

Sharing the best from my side...

--Prashant--
 
PGoRule,
Thank you very much.
It works fine.
I was wondering if it is possible to get the file name only, instead of the entire path and file name?
 
In responce to your post (17 Oct 06 20:21):
I don't think that you paid attention to what i posted

get the file name only..
It is not that difficult... Just add in the combo the string after the last "/".
(use the .substring() and the .lastindexof("/") properties)
 
TipGiver,
Sorry, I misread your instructions.
I was able to get it to work.
However, I am not sure how to do the substring() and lastindexof("/").

Could I ask for some more help.
 
I was wondering...

Now that I have a list of files, how can I select one and have it open?

The idea of this is to be able to open log files on a computer.
 

' Add the following controls:
' ComboBox1
' Button1
' FolderBrowserDialog1

Public Class Form1
Dim dir_path As String = ""

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim file_name As String = ""

ComboBox1.Text = ""
ComboBox1.Items.Clear()
FolderBrowserDialog1.SelectedPath = ""

Do
FolderBrowserDialog1.ShowDialog()
dir_path = FolderBrowserDialog1.SelectedPath & "\"
Loop While FolderBrowserDialog1.SelectedPath = ""

file_name = Dir(dir_path, FileAttribute.Normal)

Do While file_name <> ""
If Is_Log_File(file_name) Then
ComboBox1.Items.Add(file_name)
End If
file_name = Dir()
Loop
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Shell("c:\windows\notepad.exe " & dir_path & ComboBox1.SelectedItem, AppWinStyle.NormalFocus)
End Sub

Private Function Is_Log_File(ByVal file_name As String) As Boolean
Dim file_ext As String = "", pos As Integer = 0, valid = False

If InStr(file_name, ".") > 0 Then
pos = file_name.LastIndexOf(".")

file_ext = LCase(Mid(file_name, pos + 2))

If file_ext = "txt" Then
valid = True
ElseIf file_ext = "log" Then
valid = True
End If
Else
' File name has no file extension
valid = True
End If

Return valid
End Function
End Class
 
how can I select one and have it open?

Code:
Try
  System.Diagnostic.Process.Start(Me.ComboBox1.Text)
Catch ex As Exception ' **
  MessageBox.Show("Cannot open that file")
End Try

 
Be careful that if you execute (start) the file you have a path somewhere.
djj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top