Title: Create Open File Dialog for Windows Mobile 5 (PocketPC) device - Part 2 Author: Greg Dubinovskiy Email: [email protected] Environment: VB.NET Keywords: OpenFileDlg, SaveFileAsDlg Level: Intermediate Description: Create "Open File" Dialog for Windows Mobile 5 (PocketPC) device Section Miscellaneous SubSection General
While working on the "Giffy" project that allows users to create animated GIF right on their PocketPC (Windows Mobile 5), I neeeded to add a sort of "Select a File to Open" dialog to this guffy project.
Previously, I created the same dialog for Smartphone (Windows Mobile 6 for Smartphone), and in PocketPC version I only added multi-filtering capabilities.
For example, if a user needs to be presented only with text files, when Filter is set to "*.txt|*.doc", or if we need to display only graphic files, then filter is set to "*.gif|*.bmp|*.jpg|*.png":
public partial class frmOpenFileDialog : Form
{
...
...
private String[] m_Filters;
private string m_Filter="*.*";
public string Filter
{
set
{
m_Filter = value;
m_Filters = m_Filter.Split('|');
}
}
...
...
} //end class
and to set this property:
...
...
frmOpenFileDialog dlg = new frmOpenFileDialog();
dlg.Filter = "*.gif|*.bmp|*.jpg|*.png";
if (dlg.ShowDialog() == DialogResult.OK)
{
...
...
Private Sub AddSubFolders(ByVal ParentPath As String)
Try
Dim ParentFolder As New DirectoryInfo(stripExtraSlash(ParentPath))
'loop in directories
Dim DirInfoComparer As DirectoryInfoComparer = New DirectoryInfoComparer()
Dim DirList() As DirectoryInfo = ParentFolder.GetDirectories()
Array.Sort(DirList, DirInfoComparer)
Dim dirInfo As DirectoryInfo
For Each dirInfo In DirList 'ParentFolder.GetDirectories()
Dim DirectoryNode As New TreeNode
DirectoryNode.Text = dirInfo.Name ' name of file or dir
DirectoryNode.Tag = dirInfo.FullName
DirectoryNode.ImageIndex = 0 'folder
trwFileExplorer.Nodes.Add(DirectoryNode)
Next
'loop in files:
Dim FileInfoComparer As FileInfoComparer = New FileInfoComparer()
If m_Filter = "" Then m_Filter = "*.*"
Dim FileList() As FileInfo = ParentFolder.GetFiles(m_Filter)
Array.Sort(FileList, FileInfoComparer)
Dim fileInfo As FileInfo
Dim AddFile As Boolean = True
For Each fileInfo In FileList 'ParentFolder.GetFiles()
Dim FileNode As New TreeNode
FileNode.Text = fileInfo.Name
FileNode.Tag = fileInfo.FullName
FileNode.ImageIndex = 1 'file
FileNode.SelectedImageIndex = 1 'file
trwFileExplorer.Nodes.Add(FileNode)
Next
Catch ex_add_subfolder As Exception
MessageBox.Show("Error showing:" & vbCrLf & _
"-----------------------------------" & vbCrLf & _
ex_add_subfolder.Message & vbCrLf & _
"-----------------------------------", _
"Siccolo SP", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End Try
End Sub
And now:
private void AddSubFolders(string ParentPath)
{
try
{
DirectoryInfo ParentFolder = new DirectoryInfo(stripExtraSlash(ParentPath));
DirectoryInfoComparer DirInfoComparer = new DirectoryInfoComparer();
DirectoryInfo[] DirList= ParentFolder.GetDirectories();
Array.Sort(DirList, DirInfoComparer);
foreach (DirectoryInfo dirInfo in DirList)
{
TreeNode dirNode = new TreeNode(dirInfo.Name);
dirNode.Tag = dirInfo.FullName;
dirNode.ImageIndex = 0;
trwFileExplorer.Nodes.Add(dirNode);
}
FileInfoComparer FileInfoComparer = new FileInfoComparer();
//if Filter - multi-search pattern, e.g. *.txt|*.doc:
FileInfo[] FileList = ParentFolder.GetFiles(m_Filters[0]);
if ( m_Filters.Length>1 )
{
for (int filter_index=1;filter_index 0)
{
//resize FileList array to accomodate more elements:
int new_size = FileList.Length + FoundFiles.Length;
FileList =(FileInfo[]) ResizeArray(FileList, new_size);
int destination_index = new_size - FoundFiles.Length;
//and copy:
Array.Copy(FoundFiles,
0,
FileList,
destination_index,
FoundFiles.Length
);
}
}
}
Array.Sort(FileList, FileInfoComparer);
foreach (FileInfo fileInfo in FileList)
{
TreeNode fileNode = new TreeNode(fileInfo.Name);
fileNode.Tag = fileInfo.FullName;
fileNode.ImageIndex = 1;
fileNode.SelectedImageIndex = 1;
trwFileExplorer.Nodes.Add(fileNode);
}
}
catch(Exception ex_add_subfolder)
{
MessageBox.Show("Failed to load [" + ParentPath + "]\n" +
"-----------------------------------\n" +
ex_add_subfolder.Message + "\n" +
"-----------------------------------",
"Open File",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
m_Filters
- array of filters (for example m_Filters[0] - {"*.gif" , "*.bmp"...etc}
FileList
- array of found files
FileList
- array of found files according to a selected search pattern
DirectoryInfo
exposes instance methods for creating, moving, and enumerating through directories and subdirectories.
DirectoryInfo.GetFiles()
method returns a file list from the current directory.
DirectoryInfo.GetFiles(searchPattern as String)
method returns a file list from the current directory matching the given searchPattern.
DirectoryInfo.Parent
gets the parent directory of a specified subdirectory.
DirectoryInfo.Name
gets the name of this DirectoryInfo instance.
DirectoryInfo.FullName
gets the full path of the directory or file.
System.Collections.IComparer
exposes a method that compares two objects and is used in conjunction with the Array.Sort and Array.BinarySearch methods. It provides a way to customize the sort order of a collection.
System.IO.Path
performs operations on String instances that contain file or directory path information.
System.IO.Path.Combine()
combines two path strings.
If you would like to read more on this story - please take a look at Siccolo - Free Mobile Management Tool For SQL Server and more articles at Siccolo Articles