About Chris Expertise I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.
Experience I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.
Education/Credentials I have some college education, but does it really matter in this field of work?
Expert: Chris Date: 8/19/2007 Subject: List box and open file dialog issues
Question QUESTION: Dear Sir,
I'm working on an ID3 tag editor for .mp3 files. One of my options is to change the file name (eg. "bandname-track number-songtitle.mp3" to "songtitle.mp3")
I wish to set this up by clinking a button which opens a 'open file dialog'. The dialog has been set to select multiple files, however when i accept it the list box only shows up the first file i select. my code is:
Private Sub btnFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFolder.Click
Dim Ofiles As String
FolderOpen.InitialDirectory = "C:\Users\Steve\Music"
FolderOpen.Multiselect = True
FolderOpen.Title = "Choose .Mp3's To Re-Tag"
FolderOpen.Filter = "Mpeg Audio Files| *.mp3"
FolderOpen.ShowDialog()
Ofiles = FolderOpen.FileName
LstList.Items.Add(Ofiles)
End Sub
i have tried to change "Ofiles = folderOpen.fileName" to end with FolderOpen.filenames but when i add the 's' it comes up with errors.
Any help would be greatly appreciated. Also, Last time I asked a question from you I had a great response and it helped me greatly so I thank you for that.
I may have 1 or 2 more questions too.
Thankyou for your time
Steve.
ANSWER: Hi,
You've got a really simple to answer question here... You can use the .FileNames property of the OpenFileDialog control, which is an array of strings, each holding a file name that was selected.
Private Sub btnFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFolder.Click
FolderOpen.InitialDirectory = "C:\Users\Steve\Music"
FolderOpen.Multiselect = True
FolderOpen.Title = "Choose .Mp3's To Re-Tag"
FolderOpen.Filter = "Mpeg Audio Files| *.mp3"
' Make sure the User clicked OK and not Cancel
If (FolderOpen.ShowDialog() = Windows.Forms.DialogResult.OK) Then
' Make sure the User selected one or more files
If (FolderOpen.FileNames.Length > 0) Then
' Loop through the list of selected filenames and add each one to your listbox
For Each strFileName As String In FolderOpen.FileNames
LstList.Items.Add(strFileName)
Next
End If
End If
End Sub
Sounds like a very interesting project you're working on there, let me know if there's anything else you need help with.
---------- FOLLOW-UP ----------
QUESTION: It worked greatly, thank you. However, I now only want to display the file name and file extension without the entire path in a list box. Once I have the file name's I wish to display the first item in the list box inside a text box. I have no idea on this one and I have looked for some time trying to find out.
thank you for reading and any help would be wonderful.
Thank you,
Steve
Answer First, let me tell you something about the ListBox control: you can add any object to it, not just strings. If you add an object to it, the string value it will show to the user of your program in the list will be whatever that object returns from a call to its .ToString() function.
Knowing this, you can build your own class to hold the full name of the file and path, as well as just the name of the file, returning the short version from .ToString(). This can be made easier through the use of the FileInfo class, in the System.IO namespace. You'll have to make sure you've got an Imports System.IO statement to use it. Then, create a new class, called MyFileInfo, which will wrap the FileInfo object, and return just the filename without path on calls to .ToString():
'--[ MyFileInfo.vb ]--
Imports System.IO
Public Class MyFileInfo
Protected m_objFI As FileInfo = Nothing
Public ReadOnly Property FileInfo() As FileInfo
Get
Return m_objFI
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return m_objFI.Name
End Get
End Property
Public ReadOnly Property FullName() As String
Get
Return m_objFI.FullName
End Get
End Property
Private Sub New()
End Sub
Public Sub New(ByVal objFI As FileInfo)
m_objFI = objFI
End Sub
Public Sub New(ByVal strFilename As String)
m_objFI = New FileInfo(strFilename)
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
'--[ end MyFileInfo.vb ]--
Then, make a slight change to your For loop:
For Each strFileName As String In FolderOpen.FileNames
LstList.Items.Add(New MyFileInfo(strFileName))
Next
Since the ToString() function has been overridden in MyFileInfo to return just the filename, this will display what you want in the ListBox. And to get the full path and filename, you can use the .FullName property of each MyFileInfo object. For example, to fill a TextBox with the full path and filename of the first item in your ListBox:
Dim objMFI as MyFileInfo = DirectCast(LstList.Items.Item(0), MyFileInfo)
TextBox1.Text = objMFI.FullName
And if you wanted to get access to the base FileInfo object:
Dim objFI as FileInfo = DirectCast(LstList.Items.Item(0), MyFileInfo).FileInfo
TextBox1.Text = objFI.FullName
There's also a DirectoryInfo class, which can be handy, especially for finding the files in a particular folder:
Dim objDI As New DirectoryInfo("C:\some folder\")
Dim arrFIs As FileInfo() = objDI.GetFiles("*.mp3")