How to customize OpenFileDialog with nested types?

Let's say I wanted to tweak OpenFileDialog

and change, for example, how the filter for file extensions works, for example in the case of this question. After I pointed out to the author of the mentioned question what is OpenFileDialog

not inherited, I got a comment with the following:

Even though OpenFileDialog is sealed (not inherited), you can use it as a nested type. For example using the property that NativeDialog will receive. Then you write your method, always using the NativeDialog property, and you're done.

My question is, can anyone provide me with some sample code on how I would continue to do something like this? I'm a bit new to the concept of nested types, so it's hard for me to figure out what happened and I searched the web and couldn't find anything specific.

Thanks!

+2


a source to share


1 answer


A nested type is another way of saying the wrapper class (I'm guessing). This will create a new class that has a private OpenFileDialog member class. Then you create all the public items you need.

So, for OpenFileDialog, you would create a class like this:



public class CustDialog
{
   private OpenFileDialog _dialog;

   public CustDialog()
   {
       //instantiate custom OpenFileDialog here
   }

   public DialogResult ShowDialog()
   {
       return _dialog.ShowDialog();
   }
}

      

You can even take this step further and inherit the inheritance class from the CommonDialog class . This allows you to use your wrapper class just like a standard dialog.

+2


a source







All Articles