By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,288 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,288 IT Pros & Developers. It's quick & easy.

Cancel errors out

P: n/a
Jay
I am new to coding so I am trying to learn. I have a save dialog box that I
am able to get to work. I can save a file and all is good. My problem is
when I hit cancel I am getting an error and I am not really sure why. Below
is what I have code for my save dialog and if I debug it errors out at
"textstreamwriter.WriteLine(rtbFontBox.Text)" but if I take that line out
then my save does not save the wanted text. Thanks for you help! : )

Dim textstreamwriter As StreamWriter

Dim responsedialogresults As DialogResult

SaveFileDialog1.InitialDirectory = Application.StartupPath

responsedialogresults = SaveFileDialog1.ShowDialog

textstreamwriter.WriteLine(rtbFontBox.Text)

textstreamwriter.Flush()

textstreamwriter.Close()

If responsedialogresults = DialogResult.Cancel Then

textstreamwriter.Close()

End If

Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
It looks like you're closing the StreamWriter twice when the user hits
Cancel. The ShowDialog method returns code execution to the following line
(in your case "textstreamwriter.WriteLine(rtbFontBox.Text)") regardless of
whether the user clicks Ok or Cancel.

Try this:

Dim textstreamwriter As StreamWriter
Dim responsedialogresults As DialogResult

SaveFileDialog1.InitialDirectory = Application.StartupPath
responsedialogresults = SaveFileDialog1.ShowDialog

If responsedialogresults = DialogResult.Cancel Then
textstreamwriter.WriteLine(rtbFontBox.Text)
textstreamwriter.Flush()
textstreamwriter.Close()
End If

"Jay" wrote:
I am new to coding so I am trying to learn. I have a save dialog box that I
am able to get to work. I can save a file and all is good. My problem is
when I hit cancel I am getting an error and I am not really sure why. Below
is what I have code for my save dialog and if I debug it errors out at
"textstreamwriter.WriteLine(rtbFontBox.Text)" but if I take that line out
then my save does not save the wanted text. Thanks for you help! : )

Dim textstreamwriter As StreamWriter

Dim responsedialogresults As DialogResult

SaveFileDialog1.InitialDirectory = Application.StartupPath

responsedialogresults = SaveFileDialog1.ShowDialog

textstreamwriter.WriteLine(rtbFontBox.Text)

textstreamwriter.Flush()

textstreamwriter.Close()

If responsedialogresults = DialogResult.Cancel Then

textstreamwriter.Close()

End If

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.