Author Topic: Visual Basic savefile error  (Read 2882 times)

0 Members and 1 Guest are viewing this topic.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Visual Basic savefile error
« 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:
Code: [Select]

    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


also saving to another file than the original does not work, where's my mistake?

Thanks in advance!
Nick

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Visual Basic savefile error
« Reply #1 on: December 17, 2011, 01:09:22 pm »
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..
« Last Edit: December 17, 2011, 01:09:32 pm by Nick »

Offline Happybobjr

  • James Oldiges
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2325
  • Rating: +128/-20
  • Howdy :)
    • View Profile
Re: Visual Basic savefile error
« Reply #2 on: December 17, 2011, 01:09:59 pm »
Sry, i got nothin
School: East Central High School
 
Axe: 1.0.0
TI-84 +SE  ||| OS: 2.53 MP (patched) ||| Version: "M"
TI-Nspire    |||  Lent out, and never returned
____________________________________________________________

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: Visual Basic savefile error
« Reply #3 on: December 17, 2011, 04:24:10 pm »
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)
Code: [Select]
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.)
« Last Edit: December 17, 2011, 04:26:21 pm by BlakPilar »