Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am trying to change folderbrowserdialog to openfiledialog in a wpf project. The problem is that when I alter the code to openfiledialog the mp3 files will not show up in the listview. I have tried writing a test program with just a listview and a button which opens the openfiledialog and it works fine, but when I add the test code to the original code it does not work. I asked the creator of the original code and he suggested that I needed to, "get the list of selected files from and populate the listview" but when I change the linq expression the application does not work as well.

Here is the original part of the code which I believe is causing the problem:
VB
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


And here is what I tried to do in order to fix it:
VB
Private Sub collectTrackInfo(ByVal targetFolder As String)
	 btnSelectDir.IsEnabled = False
	 tbTargetFolder.IsEnabled = False

      Dim mp3_files = files.SelectedValue

   _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


Thanks in advance for any help, code, or suggestions which you may provide, or if you need any additional information in order to get a better understanding please let me know.
Posted
Updated 24-Jul-11 7:07am
v2
Comments
Sergey Alexandrovich Kryukov 24-Jul-11 13:10pm    
This sample code has nothing to do with "dialog"; and it does not look like you have any dialog problems. So I see no sense in question. Not clear what you want to achieve.

Use "Improve question".
--SA
Josh Hudnell 24-Jul-11 13:16pm    
It has to be a dialog problem. Everything works right when folderbrowser dialog is in use. The only time there is a problem is when I change the dialog to openfiledialog. What I showed was a snippet of a trackinfo class which collects all the files in a target folder by using a linq expression to do so. I didn't even display the actual dialog so how can you determine whether the dialog is working fine? If you want me to show more of the code then I will do so.

To get the files selected in an OpenFileDialog, use this code:
C#
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "MP3 Files (*.mp3)|*.mp3";
dialog.FilterIndex = 1;

dialog.CheckFileExists = true;
dialog.Multiselect = true;
string[] selectedFiles = null;

if (dialog.ShowDialog() == DialogResult.OK)
{
	selectedFiles = dialog.FileNames;
}

You can then use the string array selectedFiles in your code.
 
Share this answer
 
You have actually not shown the relevant code. You cannot interchangeably use these two controls. Use FolderBrowserDialog when you want to select a folder and use OpenFileDialog when you want to select files.
 
Share this answer
 
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:

VB
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."
	  ' Do not allow the user to create new files via the FolderBrowserDialog.
	  _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)
		' Make sure the new filename fits in the column
                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

		  ' Reset to a specific width before auto-sizing
		  gvc.Width = _maxLength
		  ' This causes auto-sizing
		  gvc.Width = Double.NaN

		End If

		' Update the status line
		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 ' the length of the longest track name

	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.)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900