Cancel errors out

J

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top