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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…