Imports System.ComponentModel Imports System.IO Imports EgtUILib Public Class EgtOpenFileDialog Implements INotifyPropertyChanged Private m_FileNameTxBl As TextBlock Private m_sDirectory As String Public Property Directory As String Get Return m_sDirectory End Get Set(value As String) m_sDirectory = value End Set End Property Private m_sFilter As String Public Property Filter As String Get Return m_sFilter End Get Set(value As String) m_sFilter = value End Set End Property Private m_sFileNameFilter As Predicate(Of String) Public Property FileNameFilter As Predicate(Of String) Get Return m_sFileNameFilter End Get Set(value As Predicate(Of String)) m_sFileNameFilter = value End Set End Property Private m_sExtensions As New List(Of String) Public ReadOnly Property Extensions As List(Of String) Get Return m_sExtensions End Get End Property Private m_FileList As New List(Of String) Public Property FileList As List(Of String) Get Return m_FileList End Get Set(value As List(Of String)) m_FileList = value NotifyPropertyChanged(NameOf(FileList)) End Set End Property Private m_SelectedFile As String Public Property SelectedFile As String Get Return m_SelectedFile End Get Set(value As String) m_SelectedFile = value NotifyPropertyChanged(NameOf(SelectedFile)) End Set End Property Private m_FileName As String Public Property FileName As String Get Return m_FileName End Get Set(value As String) m_FileName = value End Set End Property #Region "Messages" Public ReadOnly Property OpenMsg As String Get Return EgtMsg(30006) ' Apri End Get End Property Public ReadOnly Property CancelMsg As String Get Return EgtMsg(30002) ' Annulla End Get End Property #End Region Public Function EgtShowDialog() As Boolean? ' se la cartella d'origine è stata impostata If String.IsNullOrWhiteSpace(m_sDirectory) Then Throw New Exception("Exception: Directory is null or white space!") End If ' e se esiste If Not IO.Directory.Exists(m_sDirectory) Then Throw New Exception("Exception: Directory doesn't exist!") End If ' se esiste la stringa filtro estensioni If Not IsNothing(m_sFilter) Then ' creo la lista delle estensioni da considerare m_sFilter = m_sFilter.ToLower m_sFilter = m_sFilter.Replace("*", "") m_sExtensions = New List(Of String)(m_sFilter.Split(","c)) For Index = m_sExtensions.Count - 1 To 0 Step -1 If Not Path.HasExtension(m_sExtensions(Index)) Then m_sExtensions.RemoveAt(Index) End If Next End If ' leggo i file che contiene Dim AllFilesInDir() As String = IO.Directory.GetFiles(m_sDirectory) 'Dim AllVerifiedFilesInDir As New List(Of String) For FileIndex = 0 To AllFilesInDir.Count - 1 Dim CurrFile As String = AllFilesInDir(FileIndex) ' se esistono delle estensioni da verificare If m_sExtensions.Count > 0 Then ' verifico se ha una delle estensioni ricercate For ExtIndex = 0 To m_sExtensions.Count - 1 If Path.GetExtension(CurrFile).ToLower = m_sExtensions(ExtIndex).ToLower Then ' verifico se esiste la regola del filtro generico If Not IsNothing(m_sFileNameFilter) Then ' verifico che la regola del filtro generico sia rispettata If m_sFileNameFilter(CurrFile) Then ' aggiungo il file alla lista m_FileList.Add(AllFilesInDir(FileIndex)) End If Else ' aggiungo il file alla lista m_FileList.Add(AllFilesInDir(FileIndex)) End If Exit For End If Next Else ' altrimenti verifico se esiste la regola del filtro generico If Not IsNothing(m_sFileNameFilter) Then ' verifico che la regola del filtro generico sia rispettata If m_sFileNameFilter(CurrFile) Then ' aggiungo il file alla lista m_FileList.Add(AllFilesInDir(FileIndex)) End If Else ' aggiungo il file alla lista m_FileList.Add(AllFilesInDir(FileIndex)) End If End If Next ' verifico se il nome file passatomi è presente nella lista If Not String.IsNullOrWhiteSpace(m_FileName) Then If Path.HasExtension(m_FileName) Then If m_FileList.Contains(m_FileName) Then SelectedFile = m_FileName ElseIf m_FileList.Contains(m_sDirectory & "\" & m_FileName) Then SelectedFile = m_sDirectory & "\" & m_FileName End If Else For FileIndex = 0 To m_FileList.Count - 1 Dim CurrFile As String = AllFilesInDir(FileIndex) If CurrFile = m_FileName Then SelectedFile = m_FileName ElseIf CurrFile = m_sDirectory & "\" & m_FileName Then SelectedFile = m_sDirectory & "\" & m_FileName End If Next End If End If ' se non ho selezionato nulla, se esiste seleziono il primo elemento If IsNothing(m_SelectedFile) AndAlso m_FileList.Count > 0 Then SelectedFile = m_FileList(0) End If ' mostro la finestra di dialogo Return Me.ShowDialog() End Function Private Sub m_FileNameTxBl_MouseDown(sender As Object, e As Windows.Input.MouseButtonEventArgs) Handles FileNameList.MouseDoubleClick Dim src As DependencyObject = VisualTreeHelper.GetParent(DirectCast(e.OriginalSource, DependencyObject)) ' verifico che venga clickato un item, non lo spazio vuoto o la scrollbar If Not TypeOf src Is ListBoxItem Then src = Utility.FindAncestor(Of ListBoxItem)(src) End If If IsNothing(src) OrElse src.[GetType]() <> GetType(ListBoxItem) Then e.Handled = True Else If Not IsNothing(m_SelectedFile) Then m_FileName = SelectedFile DialogResult = True End If End If End Sub Private Sub OpenBtn_Click(sender As Object, e As RoutedEventArgs) Handles OpenBtn.Click If Not IsNothing(m_SelectedFile) Then m_FileName = SelectedFile DialogResult = True End If End Sub Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Sub NotifyPropertyChanged(propName As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName)) End Sub End Class