Help

J

Jay

I am getting an error on my save dialog. No matter if I try and save a file
or cancel out of it. Below is what I have code.

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

Hi,

What is the error you are getting? And on which line is the error being
indicated?

Also it might be a good idea to write your code as below, os that it only
writes to the Stream if the Ok button has been pressed on the dialog

Dim textstreamwriter As StreamWriter

Dim responsedialogresults As DialogResult


SaveFileDialog1.InitialDirectory = Application.StartupPath

responsedialogresults = SaveFileDialog1.ShowDialog

if responsedialogresults DialogResult.Ok Then
textstreamwriter.WriteLine(rtbFontBox.Text)

textstreamwriter.Flush()

textstreamwriter.Close()

Else If responsedialogresults = DialogResult.Cancel Then

textstreamwriter.Close()

End If

End if
 
J

Jay

It errors out at textstreamwriter.WriteLine(rtbFontBox.Text), and when I put
your code it it errors out at textstreamwriter.Close()


the error for below is An unhandled exception of type
'System.NullReferenceException' occurred in Assignment1.exe

Additional information: Object reference not set to an instance of an
object."

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

Hi

Well you are getting a null reference since you have not created an Object
of the Type StreamWriter. In .NEt you need to explicitly create objects
before you can use them. So you need to explictly create the object
StreamWriter passing the path of the file to save the text.


So modify your code to

Dim textstreamwriter As StreamWriter = new StreamWriter("c:\filename.txt")

Dim responsedialogresults As DialogResult


SaveFileDialog1.InitialDirectory = Application.StartupPath

responsedialogresults = SaveFileDialog1.ShowDialog

if responsedialogresults DialogResult.Ok Then
textstreamwriter.WriteLine(rtbFontBox.Text)

textstreamwriter.Flush()

textstreamwriter.Close()

Else If responsedialogresults = DialogResult.Cancel Then

textstreamwriter.Close()

End If

End if
 
J

Jay

I am missing something here. When I run you code ""Dim textstreamwriter As
StreamWriter = new StreamWriter("c:\filename.txt")"" I am getting a blank
file (correct name) on my C:\. And when I change it so it is pulling data
from a rich text box (Dim textstreamwriter As StreamWriter = New
StreamWriter(rtbFontBox.SelectedText) I am getting following error

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll

Additional information: Empty path name is not legal.

What is going on here, it was working at one time! I should say I could
save but not cancel. Now I can do anything!
 
G

Guest

Jay,

When you say it was working before, where was it saving the contents of the
RichTextBox to ? The reason it was giving you an error was that you were
trying to use the StreamWriter object before creating an instance of it.
Hence the NullReference Error.

The last sample I gave you will create a filename with the path you provide
in the constructor. And then when you call a WriteLine on the StreamWriter it
will save the contents of method call into the file.

I have updated code that will take the path of the file from the SaveFile
dialog and save the contents of the textbox.

Dim textstreamwriter As StreamWriter

Dim responsedialogresults As DialogResult

SaveFileDialog1.InitialDirectory = Application.StartupPath

responsedialogresults = SaveFileDialog1.ShowDialog
if responsedialogresults = DialogResult.OK Then
' Create a new StreamWriter Object with the filename selected
textstreamwriter = new StreamWriter( SaveFileDialog1.FileName )
' Write the contents of the TextBox
textstreamwriter.WriteLine(rtbFontBox.Text)
textstreamwriter.Flush()
textstreamwriter.Close()
End if
 
J

Jay

That got it to work. Thank you so very much. Not sure what I did to mess
things up so bad but thank you. I do have one concern about the below code
you gave me. If my rich text box is blank then it errors out on me. Also I
tried to update the path and it like you did, it would create a file name 1
with no extention. It would have an IE icon for it and my text would be
inside. Any ideas? And thank you so much for your help, I have been at
this for a long time now. : )
 
G

Guest

I am glad it works for you. For the rest 2 questions I'll give you hints and
you can then fix the code.

For the emptry textbox error, change the code to check if the textbox has
text before you call the StreamWriter's WriteLine property to ensure only if
there is text you write to the file.

For the file extension, check up the SaveFileDialog's properties to set the
file extension for your files, so that the extension is appended by default.
 
J

Jay

I got it. I just wrote a if statement that checks my txt box for "" and
then if true it writes a blank file. Thanks again. : ) I am trying to
learn how to write code from an online class, it is hard sometimes because
there is no one to ask questions and I have so many. Thank you again : )
 

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