How to Populate Data in a Listbox From a Text File in Windows Phone 7


In this article we will learn to create a text file in Windows Phone and put some text in it. Then we will try to fill a Listbox with these texts.

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 to 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.

LstWnPhn1.jpg

Step II

Add the 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" ></Button
>
    </StackPanel>

Step III

Add the following namespaces to the MainPage.xaml.cs page:

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

Step IV

Write code for the "btnWriteToFile" click event in MainPage.xaml.cs to create a text file and insert a TextBox text into 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. A "using" statement is used to ensure that IsolatedStorageFileStream and StreamWriter are closed after use.

Step V

Write code for the "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.ItemsSource with the string array formed by splitting the "fileData" string.

Final Output

LstWnPhn2.jpg


Similar Articles