Results 1 to 8 of 8

Thread: Save Dialog

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    150

    Smile Save Dialog

    hi vbforums

    how do i save everything thats in a listbox and save dialog shows exatensions like .txt .text .html .htm .url types and it writes to where you save it too also check path files would be awesome

    thanks

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

    Re: Save Dialog

    There's a couple of separate, basically independent questions in there. Saving data from a listBox to a file is the same whether you use a SaveFileDialog or not. Using a SaveFileDialog is the same no matter what you're saving. This is an example of where you should break the problem down into its components. Understand the components independently and then you can use them anywhere, independent of each other.

    So, to the SaveFileDialog first. Create an instance, set its properties appropriately and then call ShowDialog. If that returns OK then the user clicked OK and you get the selected file from the FileName property. Simple. If you want to know what properties to set you should read the MSDN documentation to see what properties it has and what each one is for.

    As for saving the contents of a ListBox, it depends on what the items are and how you want to save them. I'll assume that the items are Strings and you want to save each to a line in a text file. In that case, you'd create a StreamWriter and then use a For Each loop to enumerate the items, calling WriteLine for each one.
    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
    Addicted Member
    Join Date
    Jul 2009
    Posts
    150

    Re: Save Dialog

    yea its a list box full of numbers and letters here is the code i got to far but it doesnt save
    Dim myFileDialog As SaveFileDialog = New SaveFileDialog()

    With myFileDialog

    .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    .FilterIndex = 1
    .InitialDirectory = "C:\"
    .Title = "Save File"
    .CheckFileExists = False
    End With

    If myFileDialog.ShowDialog() = DialogResult.OK Then

    RandomPassAndNumberListBox.Text.ToString()
    Console.WriteLine(myFileDialog.FileName)
    End If
    myFileDialog = Nothing
    End Sub

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

    Re: Save Dialog

    It doesn't save because you have no code to save. Did you read the last paragraph of my previous post?
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    150

    Re: Save Dialog

    i did but im still on my first application teaching me self but didnt understand what you wanted me 2 do

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

    Re: Save Dialog

    Quote Originally Posted by pillhead2007 View Post
    i ... didnt understand what you wanted me 2 do
    Step 1
    Quote Originally Posted by jmcilhinney
    you'd create a StreamWriter
    Step 2
    Quote Originally Posted by jmcilhinney
    use a For Each loop to enumerate the items
    Step 3
    Quote Originally Posted by jmcilhinney
    calling WriteLine for each one
    You don't have to worry about step 2 and step 3 until you've completed step 1. So, create a StreamWriter. If you don't know how to create a StreamWriter then you should either read the documentation or look for examples.

    http://www.google.com.au/search?q=cr...ient=firefox-a

    As you can see, there is plenty of information available.
    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

  7. #7
    Addicted Member
    Join Date
    Feb 2010
    Posts
    197

    Re: Save Dialog

    This code:

    Code:
            Dim i As Integer
    
            Dim wsw As IO.StreamWriter
            SaveFileDialog2.Title = "Save Games List"
            SaveFileDialog2.DefaultExt = ".txt"
            SaveFileDialog2.FileName = "Have List"
            SaveFileDialog2.ShowDialog()
            wsw = New IO.StreamWriter(SaveFileDialog2.FileName)
            For i = 0 To RandomPassAndNumberListBox.Items.Count - 1
                wsw.WriteLine(RandomPassAndNumberListBox.Items.Item(i))
            Next
            wsw.Close()

    Should do it, it prompts the user to choose a location and file name, then saves a .txt file containing a list of all the items in the listbox. Attach it to a click event etc (like on a button) and then test it out.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    150

    Re: Save Dialog

    thank you dude

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