I would like to prompt the user for a save file dialog when he clicks on Ok of a message box displayed. How can i do this...
A:
You could google this. If you have problem with click event. I guess you are using visual studio just double click the button on the design surface and write your code inside the handler that you go.
http://www.jonasjohn.de/snippets/csharp/save-file-dialog-example.htm
MessageBox.Show() returns DialogResult
DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
"Important Question",
MessageBoxButtons.OK);
if (result1 == DialogResult.OK)
{ //Show SaveFileDialog }
mehmet6parmak
2010-08-26 05:08:43
+1
A:
In the event handler of the button, use the following code.
DialogResult messageResult = MessageBox.Show("Save this file?", "Save", MessageBoxButtons.OKCancel);
if (messageResult == DialogResult.OK)
{
using (var dialog = new System.Windows.Forms.SaveFileDialog())
{
dialog.DefaultExt = "*.txt";
dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
string filename = dialog.FileName;
// Save here
}
}
}
Edit: If you want to get a FileStream directly you can use SaveFileDialog.OpenFile()
. This requires less permissions if you run your application in partial trust.
Albin Sunnanbo
2010-08-26 05:11:34
Hi initially i will have a Messagebox with some text . When i click on ok i would like to open save file dialog
Dorababu
2010-08-26 05:15:33
http://msdn.microsoft.com/en-us/library/519bytz3.aspx MessageBox.Show() also returns a DialogResult you can use that.
mehmet6parmak
2010-08-26 05:20:19
Added MessageBox code.
Albin Sunnanbo
2010-08-26 05:21:41
A:
I got the answer this was what i am asking for
MessageBox.Show("Save The Current File");
if (Convert.ToBoolean( DialogResult.OK ))
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = @"C:\";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string s= saveFileDialog1.FileName;
}
}
Dorababu
2010-08-26 05:25:13
First I was going to say that this won't compile, but with a closing '}' it actually does. Then I was going to say that this code does not do what you expect it to do, but then again it actually does. Strange how two wrongs can be "right". But never the less, do not ever write such confusing code again.
Albin Sunnanbo
2010-08-26 06:23:53
Yes, but the problem is the line 'if (Convert.ToBoolean( DialogResult.OK ))'. If you add more buttons to the MessageBox (like OKCancel) this will always be true since you are not comparing with the result from the MessageBox, you just check that the CONSTANT DialogResult.OK is not zero. It looks like pure accident that it works in this particular case, but as soon as you add another button to the MessageBox your code will fall apart.
Albin Sunnanbo
2010-08-26 07:33:43