Steve25
September 27th, 2009, 04:03 PM
I'm trying to make a simple program where a text file is read in and it prints the text in a textbox and the number of lines in the file. I cant get the number of lines to come up OK and get the file to display in the textbox. However I cannot get the program to do both these things in one program. Here is my code so far:
if (openFile.ShowDialog() == DialogResult.OK)
{
//Create variable for storing the number of lines
int lines = 0;
string fileName = openFile.FileName;
StreamReader objReader = new StreamReader(fileName);
//Loop until we reach the end of the file and increase the line count
while (objReader.ReadLine() != null)
{
lines++;
}
//Print the contents of the file to the textbox
txtFile.Text = objReader.ReadToEnd();
lblLines.Text = lines.ToString();
objReader.Close();
}
if (openFile.ShowDialog() == DialogResult.OK)
{
//Create variable for storing the number of lines
int lines = 0;
string fileName = openFile.FileName;
StreamReader objReader = new StreamReader(fileName);
//Loop until we reach the end of the file and increase the line count
while (objReader.ReadLine() != null)
{
lines++;
}
//Print the contents of the file to the textbox
txtFile.Text = objReader.ReadToEnd();
lblLines.Text = lines.ToString();
objReader.Close();
}