Why FolderBrowserDialog dialog does not scroll to selected folder?

前端 未结 14 1622
失恋的感觉
失恋的感觉 2020-11-30 02:28

As show in this screen shot, the selected folder is not in the view. It needs to be scrolled down to view the selected folder.

14条回答
  •  佛祖请我去吃肉
    2020-11-30 02:53

    I know this thread is WAY old, but with extension methods, this can be added to the FolderBrowserDialog.ShowDialog method, and then used repeatedly where needed.

    The sample (below) is just using the easy SendKeys method (which I hate doing, but in this case, it works well). When using the SendKeys method to jump to the selected folder in the dialog, if you are debugging this in Visual Studio, then the SendKeys call applies to the current window, which would be the active VS window. To be more foolproof and to avoid the wrong window from getting the SendKeys message, then the extension method would contain the external method calls to send messages to the specific window similar to what Marc F posted, but translated to C#.

    internal static class FolderBrowserDialogExtension
    {
        public static DialogResult ShowDialog(this FolderBrowserDialog dialog, bool scrollIntoView)
        {
            return ShowDialog(dialog, null, scrollIntoView);
        }
    
        public static DialogResult ShowDialog(this FolderBrowserDialog dialog, IWin32Window owner, bool scrollIntoView)
        {
            if (scrollIntoView)
            {
                SendKeys.Send("{TAB}{TAB}{RIGHT}");
            }
    
            return dialog.ShowDialog(owner);
        }
    }
    

提交回复
热议问题