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.
UPDATE:
Recently the main code is:
public partial class MainPage : PhoneApplicationPage
{
private FlashCard _flashCard;
// Constructor
public MainPage()
{
InitializeComponent();
// This could go under somewhere like a load new flash card button or
// menu option etc.
try
{
_flashCard = new FlashCard(@"D:\My Workspaces\Windows Phone 7 Solution\FCard\MyDocuments\EnglishFlashCard.txt");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnLeft_Click(object sender, RoutedEventArgs e)
{
DisplayPrevious();
}
private void btnRight_Click(object sender, RoutedEventArgs e)
{
DisplayNext();
}
private void DisplayNext()
{
try
{
FlashText.Text = _flashCard.GetNextLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DisplayPrevious()
{
try
{
FlashText.Text = _flashCard.GetPreviousLine();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
And this is for the class 'FlashCard':
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)
{
MessageBox.Show(ex.Message); // This line got a message while running the solution
}
}
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]; //-- This line got an error
}
public string GetNextLine()
{
// Make sure we're not at the last line already
if (_currentLine < _lines.Count - 1)
{
_currentLine++;
}
return _lines[_currentLine]; //-- This line got an error
}
}
I've got at error message while running the solution: Attempt to access the method failed: System.IO.File.Exists(System.String).
I've tried using breakpoint and while it's getting the LoadCard() method, it's directly thrown to the exception on the constructor. I've rechecked the txt path but it's true.
And I've also got an error message while hitting the 'Next' / 'Previous' button on 'return _lines[_currentLine];' line said: ArgumentOutOfRangeException was unhandled (It's occured on the GetPreviousLine() method if hitting the 'Previous' button and GetNextLine() method for the 'Next'.
Should you need more information I'm glad to provide it. :)
UPDATE 2
Here is the recent code:
public partial class MainPage : PhoneApplicationPage
{
private string path = @"D:\My Workspaces\Windows Phone 7 Solution\FCard\EnglishFlashCard.txt";
private List<string> _lines; //-- The error goes here
private int _currentLineIndex;
//private FlashCard _flashCard;
// Constructor
public MainPage()
{
InitializeComponent();
//_lines = System.IO.File.ReadLines(path).ToList();
if (File.Exists(path))
{
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
_lines.Add(line);
}
}
CurrentLineIndex = 0;
}
private void btnLeft_Click(object sender, RoutedEventArgs e)
{
this.CurrentLineIndex--;
}
private void btnRight_Click(object sender, RoutedEventArgs e)
{
this.CurrentLineIndex++;
}
private void UpdateContentLabel()
{
this.FlashText.Text = _lines[CurrentLineIndex];
}
private int CurrentLineIndex
{
get { return _currentLineIndex; }
set
{
if (value < 0 || value >= _lines.Count) return;
_currentLineIndex = value;
UpdateContentLabel();
}
}
}
I've got the error on the line marked above said: Field 'FCard.MainPage._lines' is never assigned to, and will always have its default value null.
IsolatedStorageFile.GetUserStoreForApplication()
. If this is a windows phone 7 application, perhaps add the tag to your question. You'll probably attract an avid WP7 developer.