So what I want to be able to do is read a file that has one data segment that reads like this. So far the program opens the file from a drop down menu but I am having a hard time saving them into array. I wish to be able to click a next button after opening the file (Where it prints the last three lines from the text file into the text boxes) on a form application, and it prints each information line in the text file example below into a separate text box. This is where I am having the problem.
The names and addresses are to be saved to a EmpNames Class, and then the .split()
numbers below are to be saved into their own respective Employee Class so as to be set into a series of calculations, then print the result in a text box.
1
John MerryWeather
123 West Main Street
5.00 30
There will be multiple data segments like this, but no more than 10. This is what I have so far.
public partial class Form1 : Form
{
const int MAX = 10;
public Form1()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
//Declarations:
// linesPerEmployee: Controls the number of lines to be read.
// currEmployeeLine: Controls where in the file you are reading.
Employee employee = new Employee();
NameAdd empNames = new NameAdd();
string filename = theDialog.FileName;
List<Employee> employeeList = new List<Employee>();
int linesPerEmployee = 4;
int currEmployeeLine = 0;
//parse line by line into instance of employee class
while (employeeList != null)
{
string[] filelines = File.ReadAllLines(filename);
if (filelines != null)
{
employee.EmpNum = int.Parse(filelines[0]);
empNames.Name =
}
}
Instead of reading all lines in one chunk you could read them line by line and add each line into a
List<string>
for example, to easier handle the "lines"And then loop through the employee list to parse each 4 lines into an
Employee()
Still, I agree with the comments about using a better format instead.