Checkedlistbox help

TomL

Member
Joined
Sep 22, 2009
Messages
13
Programming Experience
Beginner
Hi i am using VB2008 express:eek:

As a learning project I am trying to code a simple program that uses varied options, with no other function other than to familiarize myself

I have a textbox, radiobuttons, checked listbox, several dialogues etc. I have had a few problems but have managed to solve through trial and error

However, i have reached a point where i need some help

I would like to save the selected items within a checkedlistbox to a "*.txt" file on a button click event

Here is my code
VB.NET:
Private Sub SaveSelected_Click()

Try
            With SaveFileDialog1

                .AddExtension = True
                .CheckPathExists = True
                .CreatePrompt = False
                .OverwritePrompt = True
                .ValidateNames = True
                .ShowHelp = True
                .DefaultExt = "txt"
                .FileName = ""
                .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
                .FilterIndex = 1
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

                    For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
                        My.Computer.FileSystem.WriteAllText(.FileName, CheckedListBox1.CheckedItems(i), False)

                    Next
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
        End Try
    End Sub

On debug I check a number of lines, on button click the openfile dialogue opens and allows me to save a file, the problem i have is that the saved file only has one line which is actually the selected(highlighted) line of the checked list box instead of the checkeditems

I have changed the code to test a few things and instead of writing to file, i outputed the results to a textbox, and also messagebox with no apparent problems, so i cant see where i am going wrong

Any help with this is greatly appreciated
 
the problem i have is that the saved file only has one line
You have append parameter of WriteAllText method set to False.

Since you probably want to write to a new file I'd use a StreamWriter:
VB.NET:
Using writer As New IO.StreamWriter("filename")
    writer.WriteLine("write a line")
    writer.WriteLine("write a line")
End Using
 
Thanks JohnH

Yes i see now thanks, (string,string,boolean)

I have used streamreader on opendialogue, so will look at writer too

thanks again
 
Back
Top