/// <summary>
        /// Callback for Windows.
        /// </summary>
        /// <param name="hwnd">Window handle of the browse dialog box.</param>
        /// <param name="uMsg">Dialog box event that generated the statusMessage.</param>
        /// <param name="lParam">Value whose meaning depends on the event specified in uMsg.</param>
        /// <param name="lpData">Application-defined value that was specified in the lParam member of the BROWSEINFO structure used in the call to SHBrowseForFolder.</param>
        /// <returns>Returns 0 except in the case of BFFM_VALIDATEFAILED. For that flag, returns 0 to dismiss the dialog or nonzero to keep the dialog displayed.</returns>
        //[CLSCompliant(false)]
        private int OnBrowseEvent(IntPtr hwnd, uint uMsg, IntPtr lParam, IntPtr lpData)
        {
            BrowseForFolderMessages messsage = (BrowseForFolderMessages)uMsg;
            switch (messsage)
            {
                case BrowseForFolderMessages.BFFM_INITIALIZED:
                    // Dialog is being initialized, so set the initial parameters
                    if (!String.IsNullOrEmpty(Caption))
                        NativeMethods.SetWindowText(hwnd, Caption);

                    if (!String.IsNullOrEmpty(SelectedPath))
                    {
                        NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETEXPANDED, (IntPtr)1, SelectedPath);
                        NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETSELECTIONW, (IntPtr)1, SelectedPath);
                    }

                    if (!String.IsNullOrEmpty(OkText))
                        NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETOKTEXT, (IntPtr)0, OkText);

                    if (this.Initialized != null)
                    {
                        EventHandler<FolderBrowserDialogInitializedEventArgs> h = this.Initialized;
                        h(this, new FolderBrowserDialogInitializedEventArgs(hwnd));
                    }

                    return 0;

                case BrowseForFolderMessages.BFFM_SELCHANGED:
                    try
                    {
                        StringBuilder sb = new StringBuilder(260);
                        if (lParam == IntPtr.Zero || 0 == NativeMethods.SHGetPathFromIDList(lParam, sb))
                            return 0;
                        this.SelectedPath = sb.ToString();
                    }
                    catch { return 0; }
                    if (this.SelectedPathChanged != null)
                    {
                        PropertyChangedEventHandler h = this.SelectedPathChanged;
                        h(this, new PropertyChangedEventArgs("SelectedPath"));
                    }
                    return 0;

                case BrowseForFolderMessages.BFFM_VALIDATEFAILEDA:
                case BrowseForFolderMessages.BFFM_VALIDATEFAILEDW:
                    if (this.InvalidFolderSelected != null)
                    {
                        string folderName;
                        if (messsage == BrowseForFolderMessages.BFFM_VALIDATEFAILEDA)
                            folderName = Marshal.PtrToStringAnsi(lParam);
                        else
                            folderName = Marshal.PtrToStringUni(lParam);

                        EventHandler<InvalidFolderEventArgs> h = this.InvalidFolderSelected;
                        InvalidFolderEventArgs e = new InvalidFolderEventArgs(folderName, true);
                        h(this, e);
                        return (e.DismissDialog ? 0 : 1);
                    }
                    return 0;

                default:
                    return 0;
            }
        }
        /// <summary>
        /// Callback for Windows.
        /// </summary>
        /// <param name="hwnd">Window handle of the browse dialog box.</param>
        /// <param name="uMsg">Dialog box event that generated the statusMessage.</param>
        /// <param name="lParam">Value whose meaning depends on the event specified in uMsg.</param>
        /// <param name="lpData">Application-defined value that was specified in the lParam member of the BROWSEINFO structure used in the call to SHBrowseForFolder.</param>
        /// <returns>Returns 0 except in the case of BFFM_VALIDATEFAILED. For that flag, returns 0 to dismiss the dialog or nonzero to keep the dialog displayed.</returns>
        //[CLSCompliant(false)]
        private int OnBrowseEvent(IntPtr hwnd, uint uMsg, IntPtr lParam, IntPtr lpData)
        {
            BrowseForFolderMessages messsage = (BrowseForFolderMessages)uMsg;

            switch (messsage)
            {
            case BrowseForFolderMessages.BFFM_INITIALIZED:
                // Dialog is being initialized, so set the initial parameters
                if (!String.IsNullOrEmpty(Caption))
                {
                    NativeMethods.SetWindowText(hwnd, Caption);
                }

                if (!String.IsNullOrEmpty(SelectedPath))
                {
                    NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETEXPANDED, (IntPtr)1, SelectedPath);
                    NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETSELECTIONW, (IntPtr)1, SelectedPath);
                }

                if (!String.IsNullOrEmpty(OkText))
                {
                    NativeMethods.SendMessage(hwnd, (uint)BrowseForFolderMessages.BFFM_SETOKTEXT, (IntPtr)0, OkText);
                }

                if (Initialized != null)
                {
                    EventHandler <FolderBrowserDialogInitializedEventArgs> h = Initialized;
                    h(this, new FolderBrowserDialogInitializedEventArgs(hwnd));
                }

                return(0);

            case BrowseForFolderMessages.BFFM_SELCHANGED:
                try
                {
                    StringBuilder sb = new StringBuilder(260);
                    if (lParam == IntPtr.Zero || 0 == NativeMethods.SHGetPathFromIDList(lParam, sb))
                    {
                        return(0);
                    }
                    SelectedPath = sb.ToString();
                }
                catch { return(0); }
                if (SelectedPathChanged != null)
                {
                    PropertyChangedEventHandler h = SelectedPathChanged;
                    h(this, new PropertyChangedEventArgs("SelectedPath"));
                }
                return(0);

            case BrowseForFolderMessages.BFFM_VALIDATEFAILEDA:
            case BrowseForFolderMessages.BFFM_VALIDATEFAILEDW:
                if (InvalidFolderSelected != null)
                {
                    string folderName;
                    if (messsage == BrowseForFolderMessages.BFFM_VALIDATEFAILEDA)
                    {
                        folderName = Marshal.PtrToStringAnsi(lParam);
                    }
                    else
                    {
                        folderName = Marshal.PtrToStringUni(lParam);
                    }

                    EventHandler <InvalidFolderEventArgs> h = InvalidFolderSelected;
                    InvalidFolderEventArgs e = new InvalidFolderEventArgs(folderName, true);
                    h(this, e);
                    return(e.DismissDialog ? 0 : 1);
                }
                return(0);

            default:
                return(0);
            }
        }