C # FolderBrowserDialog Scroll to Selected Path folder selection box The initial position does not scroll to the default folder

Source: Internet
Author: User

     Public Static classFolderbrowserlauncher {/// <summary>        ///Using the title text to look for the Top Level dialog window is fragile. ///in particular, this would fail in non-english applications. /// </summary>        Const string_toplevelsearchstring ="Browse for Folder"; /// <summary>        ///these should is more robust. We find the correct child controls in the dialog///by using the GetDlgItem method, rather than the FindWindow (Ex) method,///because the dialog item IDs should be constant. /// </summary>        Const int_dlgitembrowsecontrol =0; Const int_dlgitemtreeview = -; [DllImport ("user32.dll", SetLastError =true)]        Static externIntPtr FindWindow (stringLpclassname,stringlpwindowname); [DllImport ("user32.dll")]        Static externIntPtr GetDlgItem (IntPtr hdlg,intNiddlgitem); [DllImport ("user32.dll", CharSet =CharSet.Auto)]Static externIntPtr SendMessage (IntPtr hWnd, UInt32 Msg, IntPtr WParam, IntPtr lParam); /// <summary>        ///Some of the messages that the Tree View control would respond to/// </summary>        Private Const intTv_first =0x1100; Private Const intTvm_selectitem = (Tv_first + One); Private Const intTvm_getnextitem = (Tv_first +Ten); Private Const intTvm_getitem = (Tv_first + A); Private Const intTvm_ensurevisible = (Tv_first + -); /// <summary>        ///Constants used to identity specific items in the Tree View control/// </summary>        Private Const intTvgn_root =0x0; Private Const intTvgn_next =0x1; Private Const intTvgn_child =0x4; Private Const intTvgn_firstvisible =0x5; Private Const intTvgn_nextvisible =0x6; Private Const intTvgn_caret =0x9; /// <summary>        ///calling this method was identical to calling the ShowDialog method of the provided///FolderBrowserDialog, except that an attempt'll be made to scroll the Tree View///The currently selected folder visible in the dialog window. /// </summary>        /// <param name= "Dlg" ></param>        /// <param name= "parent" ></param>        /// <returns></returns>         Public StaticDialogResult Showfolderbrowser (FolderBrowserDialog dlg, IWin32Window parent =NULL) {DialogResult result=DialogResult.Cancel; intRetries =Ten; using(Timer t =NewTimer ()) {T.tick+ = (s, a) = =                {                    if(Retries >0)                    {                        --retries; INTPTR Hwnddlg= FindWindow ((string)NULL, _toplevelsearchstring); if(Hwnddlg! =IntPtr.Zero) {IntPtr Hwndfolderctrl=GetDlgItem (Hwnddlg, _dlgitembrowsecontrol); if(Hwndfolderctrl! =IntPtr.Zero) {IntPtr Hwndtv=GetDlgItem (Hwndfolderctrl, _dlgitemtreeview); if(Hwndtv! =IntPtr.Zero) {INTPTR Item= SendMessage (Hwndtv, (UINT) Tvm_getnextitem,NewIntPtr (Tvgn_caret), IntPtr.Zero); if(Item! =IntPtr.Zero) {SendMessage (HWNDTV, Tvm_ensu                                        Revisible, IntPtr.Zero, item); Retries=0;                                    T.stop ();                    }                                }                            }                        }                    } Else                    {                        //                        //We failed to find the Tree View control. //                        //as a fall back (and this was an uberugly hack), we'll send//some fake keystrokes to the application in a attempt to force//The Tree View to scroll to the selected item. //T.stop (); Sendkeys.send ("{Tab}{tab}{down}{down}{up}{up}");                }                }; T.interval=Ten;                T.start (); Result=dlg.            ShowDialog (parent); }            returnresult; }    }

C # FolderBrowserDialog Scroll to Selected Path folder selection box The initial position does not scroll to the default folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.