tags:

views:

54

answers:

1

Several forms from my application use the same "open file dialog". I need to duplicate a code for "open file dialog" and its settings every time. I want to add "open file dialog" to separate dll library to avoid duplication. How do I do it ?

WinForms, Visual Studio 2008.

+3  A: 

If you want to place the code into a separate dll do the following:

  • In your solution, add a new class library project for creating your dll
  • In that newly created project, add a reference to System.Windows.Forms
  • Add the code for customizing the OpenFileDialog to the class library project
  • In your main Windows Forms project add a (project) reference to your class library

However, please note that as long as you only wish to use the customized file dialog within a single application, it is not required that this code lives in a separate dll. You can simply create a class with all the customization in your main project and re-use that class in all forms:

class SpecializedOpenFileDialog 
{
    private OpenFileDialog ofd = new OpenFileDialog();

    public SpecializedOpenFileDialog()
    {
        ofd.Multiselect = false;
        ofd.Filter = "*.html";
    }

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

    public string FileName
    {
        get 
        {
            return ofd.FileName;
        }
    }
}
0xA3
Can I add a file open dialog via standard visual studio dialogs ? Or should I manually write a code ? I can add it from toolbox on any windows form, but it's not what I want ...
nik
Thanks, 0xA3 ! I will use your SpecializedOpenFileDialog
nik