954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How Read a Text File and Display it onto a TextBlock in Visual Studio (C#)

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.

mrjimoy_05
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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();
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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
        }
    }
}
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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

mrjimoy_05
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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();
bhagawatshinde
Posting Whiz
313 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 

Hope this will help you..

bhagawatshinde
Posting Whiz
313 posts since Sep 2010
Reputation Points: 21
Solved Threads: 22
 
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

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
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.

mrjimoy_05
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

File.ReadAllLines has been in since .net 2.0

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

Ketsuekiame
Master Poster
752 posts since May 2010
Reputation Points: 349
Solved Threads: 107
 

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

mrjimoy_05
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
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
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

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

mrjimoy_05
Newbie Poster
24 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: