Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Nick on December 13, 2011, 11:33:59 am
-
Hello
I'm experimenting a little bit with visual basics, as we have to learn the basics for school (but i'm going much further of course xp hmm, or maybe i want, but it won't work) and i'm stuck
i'm trying and trying to get the saveDialogBox working, but everytime i want to create a new file, it gives an error and quits the program.. here's the code:
Private Sub ToolStripButton4_Click(sender As Object, e As System.EventArgs) Handles ToolStripButton4.Click
SaveFileDialog1.Filter = "Nspire Lua files (*.lua)|*.lua|txt files (*.txt)|*.txt|All files (*.*)|*.*"
SaveFileDialog1.FilterIndex = 2
SaveFileDialog1.RestoreDirectory = True
SaveFileDialog1.CreatePrompt = True
SaveFileDialog1.OverwritePrompt = True
SaveFileDialog1.ShowDialog()
SaveFileDialog_FileOK(ToolStripButton4, AcceptButton)
End Sub
Private Sub SaveFileDialog_FileOK(sender As Object, e As System.EventArgs) Handles SaveFileDialog1.FileOk
Dim myStream As IO.Stream
Dim SaveName As String
' If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = SaveFileDialog1.OpenFile()
SaveName = SaveFileDialog1.FileName
My.Computer.FileSystem.WriteAllText(SaveName, addText.Text, False)
myStream.Close()
' End If
End Sub
and this is the error screen
(http://img.removedfromgame.com/imgs/SaveFileDialogError.gif)
also saving to another file than the original does not work, where's my mistake?
Thanks in advance!
Nick
-
isn't there anyone who can help me, i'm really stuck, and i need it xs i can't find any solution on the internet, as it seems i'm doing it good..
-
Sry, i got nothin
-
According to the error you're getting in the exception message box, you have that file open somewhere else. I'm a little rusty on my Visual Basic, but something like this should work for you: (this is the way I normally do it, if I don't split into other methods)
Private Sub ToolStripButton4_Click(sender As Object, e As System.EventArgs) Handles ToolStripButton4.Click
Dim save As New SaveFileDialog
save.Filter = "Nspire Lua files (*.lua)|*.lua|txt files (*.txt)|*.txt|All files (*.*)|*.*"
save.FilterIndex = 2
save.RestoreDirectory = True
save.CreatePrompt = True
save.OverwritePrompt = True
If save.ShowDialog() = DialogResult.Ok Then
System.IO.StreamWriter writer
Using writer As New System.IO.StreamWriter(save.FileName)
writer.Write(addText.Text)
End Using
End If
End Sub
EDIT: What "Using" does is if the class you specify to "Use" is disposable, then all of the cleanup will be handled for you (i.e. closing, disposing, etc.)