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

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


If you want to be able to read lines moving backward and forward within the file you'll either need to store all of the lines inside of an object (perhaps a List<string> or string array), or you'll have to manually reposition your cursor via a Seek method (such as FileStream.Seek). It will depend on how big the flash card file is. If it's very large (contains many lines), you may not want to store it all in memory, preferring instead the seek option.

Here is a sample loading the entire contents of the flash card:

namespace FlashReader{    public partial class Form1 : Form    {        // Hold your flash card lines in here        private List<string> _lines;        // Track your current line        private int _currentLine;        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            // Load up your file            LoadFile(@"D:\Path\To\EnglishFlashCard.txt");        }

Your load file could look something like this:

        private void LoadFile(string file)        {            using (var reader = File.OpenText(file))            {                _lines = new List<string>();                string line;                while ((line = reader.ReadLine()) != null)                {                    _lines.Add(line);                }            }            // Set this to -1 so your first push of next sets the current            // line to 0 (first element in the array)            _currentLine = -1;        }

Your previous click could look like this:

        private void btnPrevious_Click(object sender, EventArgs e)        {            DisplayPrevious();        }        private void DisplayPrevious()        {            // Already at first line            if (_currentLine == 0) return;            _currentLine--;            FlashText.Text = _lines[_currentLine];        }

Your next button click could look like this:

        private void btnNext_Click(object sender, EventArgs e)        {            DisplayNext();        }        private void DisplayNext()        {            // Already at last line            if (_currentLine == _lines.Count - 1) return;            _currentLine++;            FlashText.Text = _lines[_currentLine];        }    }}

You would want to add some error checking of course (what if the file is missing etc.).

PS - I've compiled this code using a file with the following lines and confirmed that it works:

Line one Line two Line three Line four

UPDATE:

If you want to go with something more akin to an object-oriented approach, consider creating a FlashCard class. Something like this:

public class FlashCard{    private readonly string _file;    private readonly List<string> _lines;    private int _currentLine;    public FlashCard(string file)    {        _file = file;        _currentLine = -1;        // Ensure the list is initialized        _lines = new List<string>();        try        {            LoadCard();        }        catch (Exception ex)        {            // either handle or throw some meaningful message that the card            // could not be loaded.        }    }    private void LoadCard()    {        if (!File.Exists(_file))        {            // Throw a file not found exception        }        using (var reader = File.OpenText(_file))        {            string line;            while ((line = reader.ReadLine()) != null)            {                _lines.Add(line);            }        }    }    public string GetPreviousLine()    {        // Make sure we're not at the first line already        if (_currentLine > 0)        {            _currentLine--;        }        return _lines[_currentLine];    }    public string GetNextLine()    {        // Make sure we're not at the last line already        if (_currentLine < _lines.Count - 1)        {            _currentLine++;        }        return _lines[_currentLine];    }}

Now you can instead do something like this in your main form:

public partial class Form1 : Form{    private FlashCard _flashCard;    public Form1()    {        InitializeComponent();    }    private void Form1_Load(object sender, EventArgs e)    {        // This could go under somewhere like a load new flash card button or        // menu option etc.        try        {            _flashCard = new FlashCard(@"c:\temp\EnglishFlashCard.txt");        }        catch (Exception)        {           // do something        }    }    private void btnPrevious_Click(object sender, EventArgs e)    {        DisplayPrevious();    }    private void DisplayPrevious()    {        FlashText.Text = _flashCard.GetPreviousLine();    }    private void btnNext_Click(object sender, EventArgs e)    {        DisplayNext();    }    private void DisplayNext()    {        FlashText.Text = _flashCard.GetNextLine();    }}


You might separate the parsing phase from the displaying phase.First, read each row of your file and make a list of string of it:

List<string> list = new List<string>();System.IO.StreamReader file = new System.IO.StreamReader(filePath); while(!file.EndOfStream){   string line = file.ReadLine();  list.Add(line);} Console.WriteLine("{0} lines read", list.Count);FlashText.Text = list[0];

Then, keep an id of the current item and display it in your block.

private int curId = 0;// on next button clickif (curId < list.Count - 1)  FlashText.Text = list[++curId];// on prev button clickif (curId > 0)  FlashText.Text = list[--curId];


I like the existing answers, but I think creating a class to represent a list of objects is overkill for this problem. I'd prefer to keep it simple - a list of string should just be represented by List<string>.

public partial class Form1 : Form{    private string path = @"D:\temp\test.txt";    private List<string> _lines;    private int _currentLineIndex;    public Form1()    {        InitializeComponent();        // if you're adding these using a reader then         // you need to initialize the List first...        _lines = new List<string>();        _lines = System.IO.File.ReadAllLines(path).ToList();        CurrentLineIndex = 0;    }}

Three simple methods - one to handle the Back click, one to handle the Forward click, and one to update the label.

    private void BackButton_Click(object sender, EventArgs e)    {        this.CurrentLineIndex--;    }    private void ForwardButton_Click(object sender, EventArgs e)    {        this.CurrentLineIndex++;    }    private void UpdateContentLabel()    {        this.ContentLabel.Text = _lines[CurrentLineIndex];    }

And when the CurrentLineIndex property is set, trigger the UpdateContentLabel()

    private int CurrentLineIndex    {        get {  return _currentLineIndex; }        set        {            if (value < 0 || value >= _lines.Count) return;            _currentLineIndex = value;            UpdateContentLabel();        }    }