Results 1 to 5 of 5

Thread: Listbox Saving Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    12

    Listbox Saving Issue

    I'm getting errors in this code! Why? Visual Basic 2010

    Code:
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            SaveFileDialog1.InitialDirectory = "C:\"
            SaveFileDialog1.FileName = "Save As..."
            SaveFileDialog1.Filter = ("Text Files | *.txt")
            SaveFileDialog1.ShowDialog()
    
            Dim w As New IO.StreamWriter(SaveFileDialog1.FileName)
            Dim 1 As Integer
            For i = 0 To ListBox1.Items.Count = 1
                w.WriteLine(ListBox1.Items.Item(i))
    
            Next
            w.Close()
    
    
    
    
    
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Listbox Saving Issue

    For a start, you should not be setting the FileName of the dialogue to "Save As...". That might be appropriate for the Title perhaps.

    I don't know that that will cause an error though. Maybe if you were to actually explain the errors to us. Every one has an error message so why keep those a secret from the people whom you want to help you fix them? Are you talking about compilation errors or run time errors? What lines do the errors occur on? Please provide a FULL and CLEAR description of the problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    12

    Re: Listbox Saving Issue

    I aplogize, I am getting an error on the "1" and the "i".

  4. #4
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Listbox Saving Issue

    Quote Originally Posted by lacobus View Post
    I'm getting errors in this code! Why? Visual Basic 2010

    Code:
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            SaveFileDialog1.InitialDirectory = "C:\"
            SaveFileDialog1.FileName = "Save As..."
            SaveFileDialog1.Filter = ("Text Files | *.txt")
            SaveFileDialog1.ShowDialog()
    
            Dim w As New IO.StreamWriter(SaveFileDialog1.FileName)
            Dim 1 As Integer
            For i = 0 To ListBox1.Items.Count = 1
                w.WriteLine(ListBox1.Items.Item(i))
    
            Next
            w.Close()
    
        End Sub
    End Class
    You can't use a number for an identifier. Try something like Dim i as Integer instead.

    Code:
    For i As Integer = 0 To ListBox1.Items.Count
    ...
    Last edited by Arve K.; Nov 22nd, 2012 at 05:37 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Listbox Saving Issue

    try this:

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SaveFileDialog1.InitialDirectory = "C:\"
        SaveFileDialog1.Title = "Save As..."
        SaveFileDialog1.Filter = "Text Files TXT (*.txt)| *.txt"
        SaveFileDialog1.FilterIndex = 0
    
        If SaveFileDialog1.ShowDialog() = dialogresult.ok Then
            IO.File.WriteAllLines(SaveFileDialog1.FileName, ListBox1.Items.cast(Of String).toarray)
        End If
    
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width