There may be alot of questions coming out of me for my notepad project but im going to finish it dammit!
right now i am trying to get my save feature functioning i have found several different examples on how to do it, but i am having trouble adapting it to my specific situation right now this is the code i have for the save feature.
i am getting an error on this line FileStream fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);Code:private void saveToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog savDialog = new SaveFileDialog(); savDialog.Title = "Specify Destination Filename"; savDialog.Filter = "Text Files(*.txt)\"|*.txt"; savDialog.FilterIndex = 1; savDialog.OverwritePrompt = true; FileStream fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII); sw.Write(richTextBox1.Text); sw.Flush(); sw.Close(); fs.Close(); }
it says that an empty path name is not legal when i use the save feature during debugging.
can anyone shed some light on this? essentially my goal is to get the "Save As" form to open and have the user be able to name the file and save it in the directory they choose.
richTextBox1 is the name of the textbox i am trying to save into a text file.
Your not actually opening up the dialog, just giving the path before you even select it. Add this if statement which I bolded in your new code. It will show the dialog and also only execute the save code if the user clicks "OK".
Code:SaveFileDialog savDialog = new SaveFileDialog(); savDialog.Title = "Specify Destination Filename"; savDialog.Filter = "Text Files(*.txt)\"|*.txt"; savDialog.FilterIndex = 1; savDialog.OverwritePrompt = true; if (savDialog.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII); sw.Write(richTextBox1.Text); sw.Flush(); sw.Close(); fs.Close(); }
Such a small thing. thanks soooo much QuackWare!!! yay its saves.
You dont happen to know how i can get my textbox to scale with the window? its for making a notepad.
I'm not in front of my computer right now but I think it has to do with the "anchor" property.
You can also dock textbox in parent container.
With IDisposable better use "using" keyword:
Code:using (var fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) using (var sw = new StreamWriter(fs, Encoding.ASCII)) sw.Write(richTextBox1.Text);
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks