Search code examples
c#visual-studio-2017vsix

How can I create a file picker option in a VSIX DialogPage?


I am creating an extension for Visual Studio 2017. I need an option to select a directory. I have created a DialogPage with a string option, as in https://msdn.microsoft.com/en-us/library/bb166195.aspx. It is working well but not really user-friendly. I would like to replace it with a File Picker, but don't know how to do it within a DialogPage. How can I do this?


Solution

  • If you want to host a user-created control inside an options page, it could be achieved by overriding the Window property of your DialogPage subclass:

    [BrowsableAttribute(false)] 
    protected override IWin32Window Window { 
    get { return MyUserControl; } 
    }
    

    A reference to the IWin32Window object implementing the window's client area should be returned by this property. Visual Studio requires its options pages to be constant, i.e. they should not be recreated for any of their subsequent calls. As Windows Forms objects can delete and recreate their window handles at will, we recommend passing a reference to the object derived from a UserControl type.

    Then depending if it's WPF or Winform make a UserControl with a nicer File Picker GUI, eg a OpenFileDialog Control that returns a IWin32Window implementation.

    See the Ref