76795Solution/dropbox14.zip
dropbox14/.vs/dropbox14/v16/.suo
dropbox14/dropbox14/AddHoursData.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dropbox14
{
public partial class AddHoursData : Form
{
//Complete path
static string path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
string completePath = path + "\\" + "employee.txt";
public AddHoursData()
{
InitializeComponent();
}
private void AddHoursData_Load(object sender, EventArgs e)
{
//On load fill the empid, emp name and hours worked form text file of first employee added
StreamReader sr = new StreamReader(completePath);
//Read the first line of text
string line = sr.ReadLine();
var arrayVal = line.Split(';').Select(x=>x.Split(':')).ToArray();
txtEmpID.Text = arrayVal[0][1].ToString();
txtEmpName.Text = arrayVal[1][1].ToString();
txtHoursWorked.Text = arrayVal[3][1].ToString() == "0" ?"": arrayVal[3][1].ToString();
sr.Close();
}
private void btnNext_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(completePath);
//Read the first line of text
string line = sr.ReadLine();
while (line != null && line != "")
{
//Get employee id
var employeeID = line.Substring(line.IndexOf("Employee id:") + "Employee id:".Length).Split(';')[0];
//if same as textbox then update data in list box and set texbox value to next employee
if (employeeID == txtEmpID.Text)
{
//Array by using split
var arrayVal = line.Split(';').Select(x => x.Split(':')).ToArray();
//Add value to list box which is hidden and then add to text file when click on save
lstEmployeeDetails.Items.Add("Employee id:" + txtEmpID.Text + ";Employee Name:" + txtEmpName.Text + ";Pay Rate:" + arrayVal[2][1] + ";Hours Worked:" + txtHoursWorked.Text);
line = sr.ReadLine();
//Update text box with next employee details
if (line != null && line != "")
{
arrayVal = line.Split(';').Select(x => x.Split(':')).ToArray();
txtEmpID.Text = arrayVal[0][1].ToString();
txtEmpName.Text = arrayVal[1][1].ToString();
txtHoursWorked.Text = arrayVal[3][1].ToString() == "0" ? "" : arrayVal[3][1].ToString();
}
else
{
//When no more employee left show message and disable Next button
MessageBox.Show("No more employees!!");
btnNext.Enabled = false;
}
break;
}
line = sr.ReadLine();
}
sr.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtHoursWorked.Clear();
}
private void btnSaveClose_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(completePath);
int count = Convert.ToInt32(lstEmployeeDetails.Items.Count.ToString());
int countlines = 0;
int countVal = 0;
while (sr.ReadLine() != null)
{
countlines++;
}
sr.Close();
StreamReader sr1 = new StreamReader(completePath);
//If list item count is not equal to number of element in text box then show message.
//Else update data
if (count == countlines)
{
string line = sr1.ReadLine();
string[] newLines = new string[count];
while (line != null && line != "")
{
var employeeID = line.Substring(line.IndexOf("Employee id:") + "Employee id:".Length).Split(';')[0];
foreach (var item in lstEmployeeDetails.Items)
{
var arrayVal = item.ToString().Split(';').Select(x => x.Split(':')).ToArray();
if (employeeID == arrayVal[0][1])
{
newLines[countVal] = item.ToString();
countVal++;
break;
}
}
line = sr1.ReadLine();
}
sr1.Close();
StreamWriter sw = new StreamWriter(completePath);
for (int i = 0; i < count; i++)
{
sw.WriteLine(newLines[i], true);
}
//Close the file
sw.Close();
MessageBox.Show(this, "Data saved successfully!!", "Information", MessageBoxButtons.OK);
this.Close();
}
else
{
MessageBox.Show("Enter hours worked for all employee");
}
}
private void txtHoursWorked_TextChanged(object sender, EventArgs e)
{
//Hours worked should be interger
try
{
int hoursWorked = int.Parse(txtHoursWorked.Text);
errorProvider1.SetError(txtHoursWorked, "");
}
catch
{
txtHoursWorked.Clear();
errorProvider1.SetError(txtHoursWorked, "Must be number only");
}
}
}
}
dropbox14/dropbox14/AddHoursData.Designer.cs
namespace dropbox14
{
partial class AddHoursData
{
///
/// Required designer variable.
/// private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
/// ///
true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.btnSaveClose = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnNext = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtHoursWorked = new System.Windows.Forms.TextBox();
this.txtEmpName = new System.Windows.Forms.TextBox();
this.txtEmpID = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.lstEmployeeDetails = new System.Windows.Forms.ListBox();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
this.SuspendLayout();
//
// btnSaveClose
//
this.btnSaveClose.BackColor = System.Drawing.SystemColors.ControlDark;
this.btnSaveClose.Location = new System.Drawing.Point(561, 362);
this.btnSaveClose.Name = "btnSaveClose";
this.btnSaveClose.Size = new System.Drawing.Size(86, 28);
this.btnSaveClose.TabIndex = 7;
this.btnSaveClose.Text = "Save & Close";
this.btnSaveClose.UseVisualStyleBackColor = false;
this.btnSaveClose.Click += new System.EventHandler(this.btnSaveClose_Click);
//
// btnClear
//
this.btnClear.BackColor = System.Drawing.SystemColors.ControlDark;
this.btnClear.Location = new System.Drawing.Point(355, 362);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(86, 28);
this.btnClear.TabIndex = 6;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = false;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnNext
//
this.btnNext.BackColor = System.Drawing.SystemColors.ControlDark;
this.btnNext.Location = new System.Drawing.Point(163, 362);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(86, 28);
this.btnNext.TabIndex = 5;
this.btnNext.Text = "Next";
this.btnNext.UseVisualStyleBackColor = false;
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.txtHoursWorked);
this.groupBox1.Controls.Add(this.txtEmpName);
this.groupBox1.Controls.Add(this.txtEmpID);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(154, 61);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(493, 250);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Hours Data";
//
// txtHoursWorked
//
this.txtHoursWorked.Location = new System.Drawing.Point(268, 168);
this.txtHoursWorked.Name = "txtHoursWorked";
this.txtHoursWorked.Size = new System.Drawing.Size(130, 22);
this.txtHoursWorked.TabIndex = 5;
this.txtHoursWorked.TextChanged += new System.EventHandler(this.txtHoursWorked_TextChanged);
//
// txtEmpName
//
this.txtEmpName.Location = new System.Drawing.Point(268, 110);
this.txtEmpName.Name = "txtEmpName";
this.txtEmpName.ReadOnly = true;
this.txtEmpName.Size = new System.Drawing.Size(130, 22);
this.txtEmpName.TabIndex = 4;
//
// txtEmpID
//
this.txtEmpID.Location = new System.Drawing.Point(268, 64);
this.txtEmpID.Name = "txtEmpID";
this.txtEmpID.ReadOnly = true;
this.txtEmpID.Size = new System.Drawing.Size(130, 22);
this.txtEmpID.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(66, 173);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(98, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Hours Worked:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(66, 116);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(113, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Employee Name:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(66, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(89, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Employee ID:";
//
// lstEmployeeDetails
//
this.lstEmployeeDetails.FormattingEnabled = true;
this.lstEmployeeDetails.Location = new System.Drawing.Point(668, 125);
this.lstEmployeeDetails.Name = "lstEmployeeDetails";
this.lstEmployeeDetails.Size = new System.Drawing.Size(120, 95);
this.lstEmployeeDetails.TabIndex = 8;
this.lstEmployeeDetails.Visible = false;
//
// errorProvider1
//
this.errorProvider1.ContainerControl = this;
//
// AddHoursData
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.lstEmployeeDetails);
this.Controls.Add(this.btnSaveClose);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnNext);
this.Controls.Add(this.groupBox1);
this.Name = "AddHoursData";
this.Text = "AddHoursData";
this.Load += new System.EventHandler(this.AddHoursData_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnSaveClose;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtHoursWorked;
private System.Windows.Forms.TextBox txtEmpName;
private System.Windows.Forms.TextBox txtEmpID;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListBox lstEmployeeDetails;
private System.Windows.Forms.ErrorProvider errorProvider1;
}
}
dropbox14/dropbox14/AddHoursData.resx
text/microsoft-resx
2.0
System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
17, 17
dropbox14/dropbox14/AddNewEmployee.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dropbox14
{
public partial class AddNewEmployee : Form
{
//Complete path
static string path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
string completePath = path + "\\" + "employee.txt";
int empID;
decimal payRate;
public AddNewEmployee()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
//On button clieck if either of text box is empty should return message
if (txtEmpID.Text == null || txtEmpID.Text == string.Empty ||
txtEmpName.Text == null || txtEmpName.Text == string.Empty ||
txtPayRate.Text == null || txtPayRate.Text == string.Empty)
{
MessageBox.Show("Enter values in all fields");
}
else
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamReader sr = new StreamReader(completePath);
//Read the first line of text
string line = sr.ReadLine();
//Bool value to check if employee with this employee id already exists or not
bool isAlreadyExist = false;
//Continue to read until you reach end of file
while (line != null && line != "")
{
//Get a employee id of current line we reading
var employeeID = line.Substring(line.IndexOf("Employee id:") + "Employee id:".Length).Split(';')[0];
//If textbox value matched with employee id, it should show message and break the loop
if (employeeID == txtEmpID.Text)
{
MessageBox.Show("Employee with this employee id already exist, Try Agian!!");
isAlreadyExist = true;
break;
}
line = sr.ReadLine();
}
//close the file
sr.Close();
//If doesnot already exists then it should write the data to text file
if (!isAlreadyExist)
{
StreamWriter sw = new StreamWriter(completePath, true);
sw.WriteLine("Employee id:" + txtEmpID.Text + ";Employee Name:" + txtEmpName.Text + ";Pay Rate:" + txtPayRate.Text + ";Hours Worked:" + 0, true);
//Close the file
sw.Close();
MessageBox.Show(this, "Data saved successfully!!", "Information", MessageBoxButtons.OK);
}
}
}
private void txtEmpID_TextChanged(object sender, EventArgs e)
{
//Employee id should be interger show error if not on text change
try
{
empID = Int32.Parse(txtEmpID.Text);
errorProvider1.SetError(txtEmpID, "");
}
catch
{
txtEmpID.Clear();
errorProvider1.SetError(txtEmpID, "Must be number only");
}
}
private void txtPayRate_TextChanged(object sender, EventArgs e)
{
//Pay rate should be decimal show error if not on text change
try
{
payRate = Convert.ToDecimal(txtPayRate.Text);
errorProvider2.SetError(txtPayRate, "");
}
catch
{
txtPayRate.Clear();
errorProvider2.SetError(txtPayRate, "Must be number only");
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//On click on clear button
txtEmpID.Clear();
txtEmpName.Clear();
txtPayRate.Clear();
errorProvider1.Clear();
errorProvider2.Clear();
}
private void tbtnClose_Click(object sender, EventArgs e)
{
//Close current page
this.Close();
}
}
}
dropbox14/dropbox14/AddNewEmployee.Designer.cs
namespace dropbox14
{
partial class AddNewEmployee
{
///
/// Required designer variable.
/// private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
/// ///
true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtPayRate = new System.Windows.Forms.TextBox();
this.txtEmpName = new System.Windows.Forms.TextBox();
this.txtEmpID = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.btnSave = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.tbtnClose = new System.Windows.Forms.Button();
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
this.errorProvider2 = new System.Windows.Forms.ErrorProvider(this.components);
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.errorProvider2)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
...