I'm just learning Windows Forms with C# and I'm having a lot of trouble rewriting my Console app into a window forms app. Here is my code in the Console App:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
const string FILENAME = @"C:\Users\annage\Desktop\SCHOOL\POS409\data.txt"; //path for CSV file
List<List<string>> EmployeesInfo = new List<List<string>>();
string inputLine = "";
StreamReader reader = new StreamReader(FILENAME);
int whileLoopRan = 0;
while ((inputLine = reader.ReadLine()) != null) //enters items into array
{
if (whileLoopRan == 0)
{
whileLoopRan++;
continue;
}
whileLoopRan = 1;
List<string> inputArray = inputLine.Split(new char[] { ',' }).ToList();//remove extra spaces
inputArray = inputArray.Select(x => x.Trim()).ToList();
EmployeesInfo.Add(inputArray);
}
List<EmployeeTaxes> EmployeeList = new List<EmployeeTaxes>();
foreach (List<string> EmployeeInfo in EmployeesInfo)
{
String employeeType;
employeeType = EmployeeInfo[5];
EmployeeTaxes employeeTaxType = new EmployeeTaxes();
employeeTaxType.GrossPay = double.Parse(EmployeeInfo[3]);
if (employeeType == ("W2"))
{
double taxes;
taxes = .07;
employeeTaxType.taxes = double.Parse(EmployeeInfo[3]) * .07;
employeeTaxType.totaltaxes = employeeTaxType.taxes * 12;
}
else if (employeeType == ("1099"))
{
}
EmployeeList.Add(employeeTaxType);
}
DisplayData(EmployeeList, EmployeesInfo);
Console.ReadLine();
}
private void DisplayData(List<EmployeeTaxes> employeeList, List<List<string>> employeesInfo)
{
for (int i = 0; i < employeeList.Count; i++)
{
List<string> Row = employeesInfo[i];
string fullName = Row[0];
string address = Row[1];
string employeeType = Row[5];
string developerType = Row[6];
EmployeeTaxes taxRow = employeeList[i];
double grossPay = taxRow.GrossPay;
double taxes = taxRow.taxes;
double totalGrossPay = taxRow.TotalGrossPay();
double annualTaxes = taxRow.totaltaxes;
double netPay = taxRow.Netpay();
Console.WriteLine("Welcome, !", fullName); // shows greeting and users name
Console.WriteLine("Address: ", address); //shows address entered
Console.WriteLine("Gross Pay: $", grossPay); // Shows gross pay entered
Console.WriteLine("Employee Type: ", employeeType);
Console.WriteLine("Monthly taxes are 7%"); ("Monthly Taxes Paid are: $" + taxes.ToString("N2")); // calculated the taxes paid monthly
Console.WriteLine("Annual Gross Pay: $" + totalGrossPay.ToString("N2")); // calulates gross pay * 12months
Console.WriteLine("Annual Taxes Paid: $" + annualTaxes.ToString("N2")); // calulates taxes * 12months
Console.WriteLine("NetPay: $" + netPay.ToString("N2"));
Console.WriteLine("Developer Type: ", developerType);
}
}
}
I am reading from a CSV .txt file and then I add it to a list of strings and I search for the "employee type" and then based on that value I loop it to do a calculation and then print out all the values.
I was able to read the csv file into a textbox in windows form, but I cannot figure out how to loop it and read the specific value and then continue with my calculation. here is what I have for my console windows form:
public Form1()
{
InitializeComponent();
this.listBox3.SelectionMode = SelectionMode.MultiSimple;
ReadingCSVFile();
}
public void ReadingCSVFile()
{
List<List<string>> EmployeesInfo = new List<List<string>>();
string inputLine = "";
using (StreamReader sr = new StreamReader(@"C:\Users\annage\Desktop\SCHOOL\POS409\data.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
listBox3.Items.Add(line);
List<string> inputArray = inputLine.Split(new char[] { ',' }).ToList();
inputArray = inputArray.Select(x => x.Trim()).ToList();
EmployeesInfo.Add(inputArray);
}
}
private void DisplayData(List<EmployeeTaxes> employeeList, List<List<string>> EmployeesInfo)
{
for (int i = 0; i < employeeList.Count; i++)
{
List<string> Row = EmployeesInfo[i];
string fullName = Row[0];
string address = Row[1];
string employeeType = Row[5];
string developerType = Row[6];
EmployeeTaxes taxRow = employeeList[i];
double grossPay = taxRow.GrossPay;
double taxes = taxRow.taxes;
double totalGrossPay = taxRow.TotalGrossPay();
double annualTaxes = taxRow.totaltaxes;
double netPay = taxRow.Netpay();
}
}
Where can I add my loop? I tried using as much of my code in both formats, but I know console.writeline needs to be changed for windows form, but I haven't gotten that far yet. Any assistance would be very much appreciated.