I am a newbie in Visual Studio (C#). I want to store a text read from a text file and display it on a TextBlock control, but just for a specified row. How can I do that?
I've try to search on the internet, and most of them just show the way to read and write.

I have one TextBlock (named 'FlashText'), and two Button (one for the 'Previous' button, another one is for the 'Next' button). What I want is, when I hit the 'Next' button, then the TextBlock showing a text read from a txt file on a specified row (for instance, the first row). And when I hit the 'Next' again, then the TextBlock should be show the second row text read from the file.

The purpose is to make a simple flash card. The code is here:

private void btnRight_Click(object sender, RoutedEventArgs e) { 
      string filePath = @"D:\My Workspaces\Windows Phone 7 Solution\SimpleFlashCard\EnglishFlashCard.txt"; 
      int counter = 0;
      string line; 
      System.IO.StreamReader file = new System.IO.StreamReader(filePath); 
      while((line = file.ReadLine()) != null) { 
        Console.WriteLine(line); 
        counter++; 
      } 
    } 
    
    file.Close(); 
    FlashText.Text = Console.ReadLine();

Please help. Thanks a bunch.

Instead of reading the file to the console, read the file into the FlashText.Text with something (maybe at line 6) like:

System.IO.StreamReader file = new System.IO.StreamReader(filePath); 
FlashText.Text = file.ReadToEnd();
file.Close();

Here is an example using a label and two buttons

using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        String[] theText;
        int position = 0;

        public Form1() {
            InitializeComponent();
            theText = File.ReadAllLines(@"D:\My Workspaces\Windows Phone 7 Solution\SimpleFlashCard\EnglishFlashCard.txt");
            if (theText.Length < 1) throw new FileLoadException("There is nothing in the file!");
        }

        private void BackButton_Click(object sender, EventArgs e) {
            Move(Direction.Backward);
        }

        private void NextButton_Click(object sender, EventArgs e) {
            Move(Direction.Forward);
        }

        private void Move(Direction d) {
            switch (d) {
                case Direction.Backward:
                    position--;
                    if (position < 0) position = 0;
                    break;
                case Direction.Forward:
                    position++;
                    if (position >= theText.Length) position = theText.Length - 1;
                    break;
            }

            label1.Text = theText[position];

        }

        private enum Direction {
            Forward,
            Backward
        }
    }
}

@All: Thanks for the reply :) . I really appreciated it.

@Momerath:
While I'm trying your solution, it's show an error on the code. There are an error under the 'ReadAllLines' line, "'System.IO.File' does not contain a definition for 'ReadAllLines'. What should I do? I've add the 'using system.IO;' on the top of the solution but it was the same.

Hi Try This ,

if (System.IO.File.Exists( Application .StartupPath   +"\\CustomerID.txt"))
            {
                StreamReader sr = new StreamReader( Application .StartupPath   +"\\CustomerID.txt");
            string line = null;
            do
            {
                line = sr.ReadLine();

                if ((line != null))
                {                                                                    //Read line
                    foreach (char cnt in line)
                    {
                        txtID.AppendText(cnt.ToString ());          //Read characters
                    }
                }
            } while (!(line == null));
            sr.Close();

Hope this will help you..

While I'm trying your solution, it's show an error on the code. There are an error under the 'ReadAllLines' line, "'System.IO.File' does not contain a definition for 'ReadAllLines'.

What version of .NET are you using as it is there File.ReadAllLines

What version of .NET are you using as it is there File.ReadAllLines

I am using Microsoft Visual Studio 2010 Express for Windows Phone and .Net 4.0 Framework.

File.ReadAllLines has been in since .net 2.0

Make sure you're using the full .net library and not just the client framework.

File.ReadAllLines has been in since .net 2.0

Make sure you're using the full .net library and not just the client framework.

How to check it? I've look at the About section and it is .NET 4.0 already.
Please help.. Thanks

I am using Microsoft Visual Studio 2010 Express for Windows Phone and .Net 4.0 Framework.

Windows Phone version is closer to Silverlight and does not have support for the static File.Read.. methods. See here.
You will have to use a TextReader, StreamReader or FileStream to execute the reads long hand in code.
The best of those is probably the TextReader, using the ReadLine method in a loop until you reach the end of the file.

@nick.crane: Thanks. I've try this code based on the site you gave:

theText = System.IO.Path.Combine(Environment.GetFolderPath(
           Environment.SpecialFolder.MyDocuments), "EnglishFlashCard.txt");

But I've got: 'System.Environment.SpecialFolder' does not contain a definition for 'MyDocuments' . I've tried to add folder named MyDocuments but it's the same.

That is because MS Phone only supports a subset of the Environment.SpecialFolder values. See here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.