Extending SaveFileDialog

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

Is there any way to customize a SaveFileDialog? You neither inherit from
SaveFileDialog directly since it is sealed, nor can I inherit from
FileDialog since it requires me to override an abstract method that uses a
parametertype that is not accessible (internal).

I need to put a single button into the SaveFileDialog to provide the user a
way to customize the fileformat.

Is there any way to do this without inventing a new SaveFileDialog from
scratch?
 
Just change the Filters Property to use whatever file format(s) you want restrict the user to ..

Eg

saveFileDialog.Filter = "Template (*.cs)|*.cs|All files (*.*)|*.*"
 
* "cody said:
Is there any way to customize a SaveFileDialog? You neither inherit from
SaveFileDialog directly since it is sealed, nor can I inherit from
FileDialog since it requires me to override an abstract method that uses a
parametertype that is not accessible (internal).

I need to put a single button into the SaveFileDialog to provide the user a
way to customize the fileformat.

Is there any way to do this without inventing a new SaveFileDialog from
scratch?

You will have to dig into dialog templates, implementations in VB6 can
be found here:

<http://vbaccelerator.com/article.asp?id=1422>
 
Just change the Filters Property to use whatever file format(s) you want
restrict the user to ...
Eg:

saveFileDialog.Filter = "Template (*.cs)|*.cs|All files (*.*)|*.*";

If I would be that simple: The user must have the ability to select
compression algorithm, quality and much more settings. I need a Button.
 
* "cody said:
I don't use VB and I have no clue how I can use *.frm files with visual
studio.

That's what I thought. What I wanted to say is, that you will have to
know a lot about p/invoke to find a stable solution.
 
Is there any way to do this without inventing a new SaveFileDialog
from
[...]
That's what I thought. What I wanted to say is, that you will have to
know a lot about p/invoke to find a stable solution.


I know a bit about p/invoke but Iam afraid it is not enough.
I tried to call GetSaveFileName using interop but Iam getting always error
126 (module not found)
Here is my code:


[StructLayout(LayoutKind.Sequential,Pack=1)]
unsafe struct OFN
{
public int lStructSize;
IntPtr hwndOwner;
public IntPtr hInstance;
string lpstrFilter;
string lpstrCustomFilter;
int nMaxCustFilter;
int nFilterIndex;
string lpstrFile;
int nMaxFile;
string lpstrFileTitle;
int nMaxFileTitle;
string lpstrInitialDir;
string lpstrTitle;
int Flags;
short nFileOffset;
short nFileExtension;
string lpstrDefExt;
IntPtr lCustData;
IntPtr/*LPOFNHOOKPROC*/ lpfnHook;
string lpTemplateName;
void * pvReserved;
int dwReserved;
int FlagsEx;
}

[System.Runtime.InteropServices.DllImport("Comdlg32.dll")]
extern static int GetSaveFileName(ref OFN ofn);

[System.Runtime.InteropServices.DllImport("Kernel32")]
extern static int GetLastError();

unsafe static void ShowFileDialog()
{
OFN ofn = new OFN();
ofn.lStructSize = Marshal.SizeOf(ofn);
ofn.hInstance = Marshal.GetHINSTANCE(typeof(MainWindow).Module);
int res = GetSaveFileName(ref ofn);
Console.WriteLine(res); // returns 0
Console.WriteLine(GetLastError()); // returns 126
}
 
Back
Top