locked
Read text file into Listbox in Visual Basic

    Question

  • So I have made an app, and it's my first Win8 app aka Metro app, I have it so that I can grab items in a listbox and save the items to a text file, but now I am stuck on reading the text file back into the listbox. Here is my saving... I am sure I am missing something. This is for Windows 8.1.

    Write to file:

    Public Async Sub SaveButton_Click(sender As Object, e As RoutedEventArgs) Handles SaveButton.Click


            Dim sampleFile As StorageFile = Await folder.CreateFileAsync("test.txt", CreationCollisionOption.OpenIfExists)

            For counter As Integer = 0 To MainListView.Items.Count - 1 Step +1
                Await FileIO.AppendTextAsync(sampleFile, MainListView.Items.Item(counter) + vbNewLine)
            Next


        End Sub

    Read file into list:

    Public Async Sub LoadButton_Click(sender As Object, e As RoutedEventArgs) Handles LoadButton.Click

            Dim sampleFile As StorageFile = Await folder.CreateFileAsync("test.txt", CreationCollisionOption.OpenIfExists)

            'MainListView.ItemsSource = sampleFile.OpenStreamForReadAsync.ToString

            ' For counter As Integer = 0 To 10 Step +1
            'Await FileIO.ReadTextAsync(MainListView.Items.Add, samplefile)
            ' Next

            Dim FilePath As String = folder.Path.ToString + "\test.txt"
            Dim ReadedText As String = IO.File.ReadAllText(FilePath)

        End Sub

    I commented some stuff out in the Read portion that didn't really work.

    Wednesday, April 16, 2014 7:14 PM

Answers

  • Hi Martinoj2009,

    Could you tell us more what do you mean by "I am stuck on reading the text file back into the listbox", is your problem on the file reading or the issue with writing data into listbox?

    I would suggest you to go through this tutorial for a better understanding for file reading: Quickstart: Reading and writing files, it contains some VB code snippet.

    For the Listbox part, once you have your list (probably you have to convert the steam from file to list) and save it into ListBox.Items

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, April 17, 2014 3:51 AM
    Moderator