Cameron Kloot

how do u save files. like bring up the Save file window, and specify what kind of file u save it as etc.
thanks




Re: Visual C# General Saving Files

TaylorMichaelL

SaveFileDialog is your friend (it only works with WinForms though). This displays the save file dialog. The various properties on the dialog are used to configure it. Use ShowDialog to display it and check the results to confirm how the dialog was closed. Here is an example:

SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Text Files|*.txt|All Files|*.*";
dlg.AddExtension = true;
dlg.CheckPathExists = true;
dlg.OverwritePrompt = true;
dlg.Title = "Save Your File";
if (dlg.ShowDialog() == DialogResult.OK)
{
//Save
Save(dlg.FileName);
};

Michael Taylor - 3/4/07
http://p3net.mvps.org





Re: Visual C# General Saving Files

Jay Vora

ya thr is a facility called saveDialog.

on ur toolbox ..see dialogs part go thru tht u wil get a Savedialog tool..by draginn it n drop to ur consol u will get it quite rght.

u now can see its properties n filter the extensions.






Re: Visual C# General Saving Files

Cameron Kloot

ok, thanks. but i dont really understand how to make it so if u click on a button it will make the save file window "pop up" if u get what i mean.... but i am starting to get it ;)






Re: Visual C# General Saving Files

Cameron Kloot

it was helpful i guess... but if u typed properly i would be able to understand u....;)


ok, i put in ShowDialog(); (in the code for "button1") and it seemed to work: there were know build errors.  but there was an exeption:
 ****** Exception Text **************
System.InvalidOperationException: Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog.
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at notepad.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Cameron Kloot\My Documents\Visual Studio 2005\Projects\notepad\notepad\Form1.cs:line 20
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
notepad
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Documents%20and%20Settings/Cameron%20Kloot/My%20Documents/Visual%20Studio%202005/Projects/notepad/notepad/bin/Debug/notepad.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

thanks







Re: Visual C# General Saving Files

TaylorMichaelL

In either the menu item event handler (Click) or the button Click handler in your UI put the code I initially posted. For example if you had a button called button1 and you double clicked it in the designer then you'd get the event handler below:

private void button1_click ( object sender, EventArgs e )
{
}

Put the code I initially posted in this handler, compile and run and then click the button. Menu items work similarily.

Michael Taylor - 3/5/07
http://p3net.mvps.org





Re: Visual C# General Saving Files

Cameron Kloot

yah, that is what i did, but it ddnt run. i dont know y. i did everything u said, and tried other stuff, it ddnt work... it had that exception






Re: Visual C# General Saving Files

TaylorMichaelL

Given the exception, are you calling ShowDialog on the parent form or the dialog itself

...
ShowDialog(); //Calls the owning forms version

...
SaveFileDialog dlg = new SaveFileDialog();
...
dlg.ShowDialog(); //Calls method on the save file dialog

If you call the former then you'll see the exception you gave. If you call the later then the save file dialog should appear. If this still doesn't work then please post the code you're using to configure and display the dialog.

Michael Taylor - 3/6/07
http://p3net.mvps.org





Re: Visual C# General Saving Files

Cameron Kloot

ok, it says that the name "dlg" does not exist in the current context when i tried the dlg.SaveDialog(); that u said. i really dont get it, but thanks for helping so much.

this is what i did. first i posted in the code from your first post, there was an error that had to do with the save part at the last little bit. i commented that out and it was fine, but still nothing interms of the dialog. another thing that i tried was to double click on the SaveFileDialog1 in the form display tab, then post the code from your first post in that, and then try the dlg.SaveDialog();, it ddnt work.






Re: Visual C# General Saving Files

TaylorMichaelL

The code that I first posted will work. The Save method is some method that you need to write to actually save the data. If the code didn't work then what happened What kind of app is it by the way Can you post the code you're using

Michael Taylor - 3/7/07




Re: Visual C# General Saving Files

Cameron Kloot

sweet, i get it. (i feel like an idiot :P) i ddnt put the code from ur first post in with the dlg.ShowDialog(); that was what i was doing wrong. but the save thing still doesnt work: when i leave the
if (dlg.ShowDialog() == DialogResult.OK)
{
//Save
Save(dlg.FileName);
};
in the code it has this error: "The name Save does not exist in the current context."
thanks