Post: [C#] Open and Save Dialogs [Snippet]
02-18-2011, 03:50 AM #1
Rath
Today Will Be Different
(adsbygoogle = window.adsbygoogle || []).push({}); Well I recently have seen quite a few people start coding C#... and also seen a bit of people using Visual Basic to make C# Windows Forms. So I thought I'd post a Snippet of my code to open and save files.

Open File
    private void Open_Click(object sender, EventArgs e)
{
string Chosen_File = "";

openFD.InitialDirectory = "C:";
openFD.Title = "Choose your FF";
openFD.FileName = "";
openFD.Filter = "FF Files|*.ff";

if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You have cancelled the operation");
}
else
{
Chosen_File = openFD.FileName;
}
}


Save File
     private void button5_Click(object sender, EventArgs e)
{
string Chosen_File = "";

saveFD.InitialDirectory = "C:";
saveFD.Title = "Save your FF";
saveFD.FileName = "";
saveFD.Filter = "FF Files|* .ff";

if (saveFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You have cancelled the operation");
}
else
{
Chosen_File = saveFD.FileName;
}
}


Now you can change values here. As in what type of files you want to open. Your title. And your directory.

This is a disclaimer... also I wouldn't include where its says things like "Open_Click" etc. Since your's might be a menu strip etc.. And change my "openFD" to the name of you save directory etc.
Not sure if you guys know this, but you can change names of buttons etc. If you don't know how, I will show you if that problem arises.

Hope some people find this useful.
Last edited by Rath ; 02-18-2011 at 04:53 AM.

The following 2 users say thank you to Rath for this useful post:

arab707, TheSpoken
02-18-2011, 04:52 AM #2
TheSpoken
Questions are answered.
Go Rath!
1st post on a good thread Winky Winky
02-18-2011, 05:01 AM #3
Rath
Today Will Be Different
Originally posted by xTheSpoken View Post
Go Rath!
1st post on a good thread Winky Winky


Thanks lol, I try C:
02-19-2011, 02:07 PM #4
Originally posted by Fascism View Post
Well I recently have seen quite a few people start coding C#... and also seen a bit of people using Visual Basic to make C# Windows Forms. So I thought I'd post a Snippet of my code to open and save files.

Open File
    private void Open_Click(object sender, EventArgs e)
{
string Chosen_File = "";

openFD.InitialDirectory = "C:";
openFD.Title = "Choose your FF";
openFD.FileName = "";
openFD.Filter = "FF Files|*.ff";

if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You have cancelled the operation");
}
else
{
Chosen_File = openFD.FileName;
}
}


Save File
     private void button5_Click(object sender, EventArgs e)
{
string Chosen_File = "";

saveFD.InitialDirectory = "C:";
saveFD.Title = "Save your FF";
saveFD.FileName = "";
saveFD.Filter = "FF Files|* .ff";

if (saveFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You have cancelled the operation");
}
else
{
Chosen_File = saveFD.FileName;
}
}


Now you can change values here. As in what type of files you want to open. Your title. And your directory.

This is a disclaimer... also I wouldn't include where its says things like "Open_Click" etc. Since your's might be a menu strip etc.. And change my "openFD" to the name of you save directory etc.
Not sure if you guys know this, but you can change names of buttons etc. If you don't know how, I will show you if that problem arises.

Hope some people find this useful.
Hi Wen I Do This I Get 24 Errors Could You Help Me I want To be able to browse all my pc and it open .SWF flash movies thank you bye
02-19-2011, 04:57 PM #5
Rath
Today Will Be Different
Originally posted by EMIN3M
Hi Wen I Do This I Get 24 Errors Could You Help Me I want To be able to browse all my pc and it open .SWF flash movies thank you bye


I'm going on a hit and miss here. My dialogs are "openFD" and "saveFD"
since I change the name of mine. I'm going to guess that yours are "openFileDialog1" and "saveFileDialog1"

Where my openFD and saveFD's are just replace them with "saveFileDialog1" and "openFileDialog1"

Also where it says "FF" change it to ".SWF" files.

---------- Post added at 12:57 PM ---------- Previous post was at 12:53 PM ----------

Originally posted by EMIN3M
Hi Wen I Do This I Get 24 Errors Could You Help Me I want To be able to browse all my pc and it open .SWF flash movies thank you bye


Oh and another thing is, don't copy the first line where it says "private void" since you might not be using buttons, since you could be using a menu bar.
02-19-2011, 08:17 PM #6
Originally posted by Fascism View Post
I'm going on a hit and miss here. My dialogs are "openFD" and "saveFD"
since I change the name of mine. I'm going to guess that yours are "openFileDialog1" and "saveFileDialog1"

Where my openFD and saveFD's are just replace them with "saveFileDialog1" and "openFileDialog1"

Also where it says "FF" change it to ".SWF" files.

---------- Post added at 12:57 PM ---------- Previous post was at 12:53 PM ----------



Oh and another thing is, don't copy the first line where it says "private void" since you might not be using buttons, since you could be using a menu bar.


Okay its now got 17 errors mostly syntax meaning to manny open brackets or incorrect stuff
02-19-2011, 08:29 PM #7
Rath
Today Will Be Different
Originally posted by EMIN3M
Okay its now got 17 errors mostly syntax meaning to manny open brackets or incorrect stuff


Did you drag and drop the "openFileDialog" and "saveFileDialog" from the toolbox.... Here is a non-renamed code for you.

    
private void yourtoolboxthinghere(object sender, EventArgs e)
{
string Chosen_File = "";

saveFileDialog1.InitialDirectory = "C:";
saveFileDialog1.Title = "Save your FF";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "FF Files|* .ff";

if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You have cancelled the operation");
}
else
{
Chosen_File = saveFileDialog1.FileName;
}


Just repeat that for openFileDialog1
Last edited by Rath ; 02-19-2011 at 08:51 PM.
02-19-2011, 08:40 PM #8
Originally posted by Fascism View Post
Did you drag and drop the "openFileDialog" and "saveFileDialog" from the toolbox.... Here is a non-renamed code for you.

    
private void yourtoolboxthinghere(object sender, EventArgs e)
{
string Chosen_File = "";

saveFileDialog1.InitialDirectory = "C:";
saveFileDialog1.Title = "Save your FF";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "FF Files|* .ff";

if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("You have cancelled the operation");
}
else
{
Chosen_File = saveFileDialog.FileName;
}


Just repeat that for openFileDialog1
3 error's

ERROR:1

Chosen_File is not declared and may be inaccesable due to its protection level

ERROR:2

Chosen_File is not declared and may be inaccesable due to its protection level

ERROR:3

Refferance to a none shared member requires an object referance
02-19-2011, 08:51 PM #9
Rath
Today Will Be Different
Originally posted by EMIN3M
3 error's

ERROR:1

Chosen_File is not declared and may be inaccesable due to its protection level

ERROR:2

Chosen_File is not declared and may be inaccesable due to its protection level

ERROR:3

Refferance to a none shared member requires an object referance


For errors 1 and 2... I noticed if you copied that last code you'd get an error...

       else
{
Chosen_File = saveFileDialog1.FileName;
}


That should fix errors one and two. I forgot to add the "1" to the end lol
02-19-2011, 08:58 PM #10
Originally posted by Fascism View Post
For errors 1 and 2... I noticed if you copied that last code you'd get an error...

       else
{
Chosen_File = saveFileDialog1.FileName;
}


That should fix errors one and two. I forgot to add the "1" to the end lol


how would i link that to open the file on a this is the code i got working

Chosen_File = ""

OpenFileDialog1.InitialDirectory = "C:"
OpenFileDialog1.Title = "Open your SWF"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "SWF Files|* .SWF"

If (OpenFileDialog1.ShowDialog() = DialogResult.Cancel) Then

MessageBox.Show("You have cancelled the operation")

Else

End If

Chosen_File = OpenFileDialog1.FileName
Last edited by (EMIN3M) ; 02-19-2011 at 09:03 PM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo