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

Introduction

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":



Code

Now, the code part. Same as Open File Dialog for Smartphone version, but with few extra lines of code.

First, the "Filter" property:
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)
        {
		...
		...


Before, in the previous version, list of files is being populated with simple GetFiles():

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);
            }



        }