I understand that I did not show the openfiledialog code because I felt it is already understood what should be in that field already, instead I chose to ask my question about the part of the code which I thought was producing the problem. I talked to the creator of the code and he said that it was very possible to change the folderbrowserdialog to openfiledialog which I believe as well. I understand the purpose and differences between folderbrowserdialog and openfiledialog also.
For the sake of argument here is the original code with the folderbrowserdialog included:
Imports System.Text
Imports System.IO
Imports System.Globalization
Imports System.Collections.ObjectModel
Namespace MP3DurationCalculator
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
_folderBrowserDialog = New FolderBrowserDialog()
_folderBrowserDialog.Description = "Select the directory that containd the MP3 files."
_folderBrowserDialog.ShowNewFolderButton = False
_folderBrowserDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory
Dim dt = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim start = System.IO.Path.Combine(dt, "Downloads")
_folderBrowserDialog.SelectedPath = start
_results = New ObservableCollection(Of TrackInfo)()
files.ItemsSource = _results
End Sub
Private Sub collectTrackInfo(ByVal targetFolder As String)
btnSelectDir.IsEnabled = False
tbTargetFolder.IsEnabled = False
Dim mp3_files = From f In Directory.GetFiles(targetFolder)
Where System.IO.Path.GetExtension(f) = ".mp3"
Select f
mp3_files = mp3_files.Reverse()
_fileCount = mp3_files.Count()
_results.Clear()
If _fileCount = 0 Then
status.Text = "No MP3 files in this folder."
btnSelectDir.IsEnabled = True
tbTargetFolder.IsEnabled = True
Else
_trackDurations = New TrackDurations(mediaElement, mp3_files, AddressOf onTrackInfo)
End If
End Sub
Private Sub btnSelectDir_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim r As DialogResult = _folderBrowserDialog.ShowDialog()
If r = Forms.DialogResult.OK Then
tbTargetFolder.Text = Me._folderBrowserDialog.SelectedPath
End If
End Sub
Private Sub onTrackInfo(ByVal ti As TrackInfo)
If ti Is Nothing Then
_maxLength = 0
_trackDurations.Dispose()
status.Text = "Ready."
btnSelectDir.IsEnabled = True
tbTargetFolder.IsEnabled = True
Else
_results.Add(ti)
Dim ft = New FormattedText(ti.Name, CultureInfo.GetCultureInfo("en-us"), Windows.FlowDirection.LeftToRight, New Typeface(files.FontFamily, files.FontStyle, files.FontWeight, files.FontStretch), files.FontSize, System.Windows.Media.Brushes.Black)
If ft.Width > _maxLength Then
_maxLength = ft.Width
Dim gv = CType(files.View, GridView)
Dim gvc = gv.Columns(0)
Dim curWidth = gvc.Width
gvc.Width = _maxLength
gvc.Width = Double.NaN
End If
Dim st = String.Format("Collecting track info {0}/{1} ...", _results.Count, _fileCount)
status.Text = st
End If
End Sub
Private Sub files_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
Dim tp = New TimeSpan()
For Each f In files.SelectedItems
tp = tp + (CType(f, TrackInfo)).Duration
Next f
Dim d = New Date(tp.Ticks)
Dim format As String = "mm:ss"
If tp.Hours > 0 Then
format = "hh:mm:ss"
End If
total.Text = d.ToString(format)
End Sub
Private Sub SelectAll_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
files.SelectAll()
End Sub
Private Sub UnselectAll_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
files.UnselectAll()
End Sub
Private Sub tbTargetFolder_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
If Directory.Exists(tbTargetFolder.Text) Then
collectTrackInfo(tbTargetFolder.Text)
End If
End Sub
Private _folderBrowserDialog As FolderBrowserDialog
Private _trackDurations As TrackDurations
Private _results As ObservableCollection(Of TrackInfo)
Private _fileCount As Integer = 0
Private _maxLength As Double = 0
End Class
<ValueConversion(GetType(TimeSpan), GetType(String))>
Public Class DurationConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
Dim duration = CType(value, TimeSpan)
Dim d = New Date(duration.Ticks)
Dim format As String = "mm:ss"
If duration.Hours > 0 Then
format = "hh:mm:ss"
End If
Return d.ToString(format)
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
End Namespace
All I need help with is the collectTrackInfo class, more specifically
what linq expression or alternative code can I use to get only the selected files from openfiledialog instead of getting all files in the targetFolder as the linq expression in the provided code calls for. Thanks in advance...
(I already know that I did not change folderbrowser dialog, this is the original code before any modifications.)