Add open file dialog to dll library
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开发者_StackOverflow社区 Studio 2008.
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;
}
}
}
精彩评论