I am new to the OpenFileDialog function, but have the basics figured out. What I need to do is open a text file, read the data from the file (text only) and correctly place the data into separate text boxes in my application. Here's what I have in my "open file" event handler:
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)
{
MessageBox.Show(theDialog.FileName.ToString());
}
}
The text file I need to read is this (for homework, I need to read this exact file), It has an employee number, name, address, wage, and hours worked:
1
John Merryweather
123 West Main Street
5.00 30
In the text file I was given, there are 4 more employees with info immediately after this in the same format. You can see that the employee wage and hours are on the same line, not a typo.
I have an employee class here:
public class Employee
{
//get and set properties for each
public int EmployeeNum { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public double Wage { get; set; }
public double Hours { get; set; }
public void employeeConst() //constructor method
{
EmployeeNum = 0;
Name = "";
Address = "";
Wage = 0.0;
Hours = 0.0;
}
//Method prologue
//calculates employee earnings
//parameters: 2 doubles, hours and wages
//returns: a double, the calculated salary
public static double calcSalary(double h, double w)
{
int OT = 40;
double timeandahalf = 1.5;
double FED = .20;
double STATE = .075;
double OThours = 0;
double OTwage = 0;
double OTpay = 0;
double gross = 0; ;
double net = 0;
double net1 = 0;
double net2 = 0;
if (h > OT)
{
OThours = h - OT;
OTwage = w * timeandahalf;
OTpay = OThours * OTwage;
gross = w * h;
net = gross + OTpay;
}
else
{
net = w * h;
}
net1 = net * FED; //the net after federal taxes
net2 = net * STATE; // the net after state taxes
net = net - (net1 + net2);
return net; //total net
}
}
So I need to pull the text from that file into my Employee class, then output the data to the correct textbox in the windows forms application. I am having trouble understanding how to do this right. Do I need to use a streamreader? Or is there another, better way in this instance? Thank you.
for this approach, you will need to add system.IO to your references by adding the next line of code below the other references near the top of the c# file(where the other using ****.** stand).
this next code contains 2 methods of reading the text, the first will read single lines and stores them in a string variable, the second one reads the whole text and saves it in a string variable(including "\n" (enters))
both should be quite easy to understand and use.
To split the text you can use .Split(" ") and use a loop to put the name back into one string. if you don't want to use .Split() then you could also use foreach and ad an if statement to split it where needed.
to add the data to your class you can use the constructor to add the data like:
or you can add it using the set by typing .variablename after the name of the instance(if they are public and have a set this will work). to read the data you can use the get by typing .variablename after the name of the instance(if they are public and have a get this will work).
Here's one way:
Modified from here:MSDN OpenFileDialog.OpenFile
EDIT Here's another way more suited to your needs: