|
-
November 26th, 2011, 10:08 AM
#1
strange problem with saveToolStripMenuItem_Click
hello there! Im having a strange problem with saving files. im trying to use saveToolStripMenuItem and saveFileDialog to save an array of strings into a text file. when i debug the program everything's fine but the save option doesnt work. i mean whenever i click on "save" nothing happens. to be precise im pasting my code here.
Code:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string saved_file = "";
saveFileDialog1.FileName = "";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
saved_file = saveFileDialog1.FileName;
string[] tarray = new string[4];
tarray[0] = "Name: " ;
tarray[1] = "Account no: " ;
tarray[2] = "Balance: " ;
tarray[3] = "Contact: " ;
File.WriteAllLines(saved_file, tarray);
}
}
if i open up a new project and paste this code it works but when im trying to implement that same thing in this current project its not working. one more thing i have done this thing before with this project and it worked.but after restarting visual c# its not working. please help me
thanks in advance
lamiajoyee
-
November 26th, 2011, 12:31 PM
#2
Re: strange problem with saveToolStripMenuItem_Click
First, that code is going to show the SaveFileDialog window twice (because you call .ShowDialog() twice). Instead do:
Code:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string saved_file = "";
saveFileDialog1.FileName = "";
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
saved_file = saveFileDialog1.FileName;
string[] tarray = new string[4];
tarray[0] = "Name: " ;
tarray[1] = "Account no: " ;
tarray[2] = "Balance: " ;
tarray[3] = "Contact: " ;
File.WriteAllLines(saved_file, tarray);
}
}
Changes bolded and underlined. It saves the result of showing the dialog box and then compares it against the result we want to enforce (OK) instead of checking to make sure it isn't cancel.
Second, the problem is you don't have the Click event of the saveToolStripMenuItem bound to this method. This link should help you get the event bound to that subroutine: http://www.techrepublic.com/article/...s-in-c/1050284
Hope that helps!
Last edited by BioPhysEngr; November 26th, 2011 at 12:32 PM.
Reason: fix bbcode
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
November 26th, 2011, 01:37 PM
#3
Re: strange problem with saveToolStripMenuItem_Click
thank you very much for your help. yes my problem has been resolved and you have cleared yet another confusion of mine which was that i didnt understand why the savefile dialogbox was appearing twice.thank you
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|