How to populate data in a Listbox from a text file in Windows Phone

Introduction:

In this article, first we will see how to create a file and insert some text in it. Then, we will retrieve these text in a ListBox with each word in the text file as an item of the Listbox.

In Windows Phone, we do not have traditional access of the OS file system. So, to store and retrieve data on the device we need to use isolated storage. Isolated storage ensures that the file created by one application is not accessible by any other applications. This storage space is isolated from other applications.

Step I:

Create a new Windows Phone Application using Visual Studio 2010 Express for Windows Phone.

Step II:

Add following code inside the Grid named ContentPanel in MainPage.xaml to add required controls.

<!--ContentPanel - place additional content here-->
<StackPanel x:Name="Panel1" Background="Gray">
     <TextBox x:Name="txtFileData" BorderBrush="Blue" BorderThickness="5" Height="200">
     </TextBox>
     <Button x:Name="btnWriteToFile" Content="Write to file" Width="220" Foreground="White"
          Background="Blue" HorizontalAlignment="Right" Click="btnWriteToFile_Click"/>
     <ListBox x:Name="listBox1" Height="200" Width="460" HorizontalAlignment="Center"
          VerticalAlignment="Top" BorderThickness="5" Padding="5" BorderBrush="Blue" Background="Black"/>
     <Button x:Name="btnFillListBox" Content="Read from file" Width="220" Foreground="White"
          Background="Blue" HorizontalAlignment="Right" Click="btnFillListBox_Click"/>
</StackPanel>

Step III:

Add following namespaces in the MainPage.xaml.cs page

using System.IO;
using System.IO.IsolatedStorage;

Step IV:

Write code for ‘btnWriteToFile’ click event in MainPage.xaml.cs to create a text file and insert Textbox text in it


private void btnWriteToFile_Click(object sender, RoutedEventArgs e)
{
     try
     {
          IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
          using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("deepak.txt", FileMode.Create, file))
          {
               using (StreamWriter writer = new StreamWriter(stream))
               {
                    writer.Write(txtFileData.Text);
               }
          }
          MessageBox.Show("Write complete!");
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }
}

Here, we are using IsolatedStorageFile. GetUserStoreForApplication to get isolated storage to be used by the application. Then, we are using IsolatedStorageFileStream to create a file. To write TextBox data to the file we are using StreamWriter class. ‘using’ is used to ensure that IsolatedStorageFileStream and StreamWriter are closed after use.

Step V:

Write code for ‘btnFillListbox’ click event in MainPage.xaml.cs to retrieve text from the text file and populate the Listbox

private void btnFillListBox_Click(object sender, RoutedEventArgs e)
{
     try
     {
          string fileData;
          IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
          using (IsolatedStorageFileStream isoStream =new IsolatedStorageFileStream("deepak.txt", FileMode.Open, file))
          {
               using (StreamReader reader = new StreamReader(isoStream))
               {
                    fileData = reader.ReadToEnd();
               }
          }
          listBox1.ItemsSource = fileData.Split(' ');
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.Message);
     }
}

Here, we are using IsolatedStorageFileStream to open the file and a StreamReader to read all the text into a string named ‘fileData’. Then we are assigning Listbox.ItemsSourcewith the string array formed by splitting ‘fileData’ string.

Final Output :

Leave a Reply

Your email address will not be published. Required fields are marked *