Question

I am trying to fill a visual studio 2012 textbox with the data from an external text file and after this, when you execute the programme, you should be able to write changes to this TextBox and save them there, but i get the error, illustrated in the screenshot. Moreover, I am running Windows 8 on a virtual machine !

[screenshot]: https://i.stack.imgur.com/zFSi4.png "Screenshot

Here is the codefor filling the textbox:

private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        LoadWords(@"Assets\AdminPageKS1words.txt");

    }

    async private void LoadWords(string filename)
    {
        var wordList = new List<String>();
        // this method reads line separated words from a text file and populates a List object // 
        Windows.Storage.StorageFolder localFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        // begin the file read operation 
        try
        {
            // open and read in the word list into an object called words 

            StorageFile sampleFile = await localFolder.GetFileAsync(filename);
            var words = await FileIO.ReadLinesAsync(sampleFile);
            // add each word returned to a list of words declared 
            // globally as List wordList = new List();
            foreach (var word in words) 
            { 
                wordList.Add(word); 
            }
            List1.ItemsSource = wordList;
        }
        catch (Exception)
        {
            // handle any errors with reading the file
        } 

And here is the code for the SAVE button:

async private void SaveButton_Click(object sender, RoutedEventArgs e)
    {

 // locate the local storage folder on the device
  Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

// create a new text file in the local folder called “File.txt”
StorageFile sampleFile = await localFolder.CreateFileAsync("File.txt",CreationCollisionOption.ReplaceExisting);

// write text to the file just created – text comes from a textblock called wordlistBox
await FileIO.WriteTextAsync(sampleFile, List1.Text); 

 // display a message saying that file is saved.
messageLabel.Text = keystage + "File saved";

    }

    public string keystage { get; set; }
Was it helpful?

Solution

As the error message says, List1 (a TextBox) doesn't have an ItemsSource property. It has a Text property.

But that's not the only problem. Because your wordList object is an IList. So you'll need to turn that into a plain string.

One solution would be to do this:

List1.Text = string.Join(Environment.NewLine, wordlist);

which would concatenate all of the lines together with line breaks in between.

Also make sure that your TextBox is configured properly with AcceptsReturn set to true so that it will display the line breaks.

OTHER TIPS

As I see List1 is a TextBox , rather than a ListBox, try:

.......
string text="";
foreach (var word in words) 
            { 
                text+=word+" "; 
            }
      List1.Text=text;

Alternatively you can define a ListBox listbox1:

 foreach (var word in words) 
            { 
                wordList.Add(word); 
            }
            listbox1.ItemsSource = wordList;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top