How to customize an OpenFileDialog using nested types?
Say I wanted to customize an OpenFileDialog
and change, for example, the way the filter for file extensions work, as in the case of this question. After I pointed out to the author of said question that the OpenFileDialog
is not inheritable, I got a comment with the following:
Even though the OpenFileDialog is sealed (not inheritable), you may use it as a nested type. For instance, using a property that will get the NativeDialog. Then, you write your method always using the NativeDialog property and you're done.
My question is, can someone provide me with an example code on how would I proceed on doing something like that? I'm kind of new to the concept of nested types so I'm having a hard time figuring that out by myself, and I searched aroun开发者_JAVA技巧d the web and couldn't find anything too concrete about it.
Thanks!
Nested type is just another way of saying wrapper class (I am assuming). So you would create a new class that has a private member class of OpenFileDialog. Then you create all of the public members that 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 one step further and have the wrapper class inherit from the CommonDialog class. This would allow you to use your wrapper class exactly like a standard dialog.
精彩评论