You are viewing our Forum Archives. To view or take place in current topics click here.
Edit details form not loading details C#
Posted:
Edit details form not loading details C#Posted:
Status: Offline
Joined: Jan 18, 201113Year Member
Posts: 328
Reputation Power: 16
Status: Offline
Joined: Jan 18, 201113Year Member
Posts: 328
Reputation Power: 16
Implementing a option to edit employee details, however the employees details wont load into the form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HRApplication
{
public partial class MainForm : Form
{
// The file used to store employee details
string employeesFile = "employees.txt";
// The collection used to hold the employee data
Employees employees;
HourlyEmployee hourlyEmployee;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
employees = new Employees();
if (!employees.Load(employeesFile))
{
MessageBox.Show("Unable to load employees file");
}
else
{
PopulateListBox();
}
}
private void PopulateListBox()
{
listBoxEmployees.Items.Clear();
//employees.Sort();
foreach (Employee employee in employees)
{
listBoxEmployees.Items.Add(employee.LastName + ", " + employee.FirstName);
}
listBoxEmployees.SelectedIndex = 0;
}
private void listBoxEmployees_DoubleClick(object sender, EventArgs e)
{
var name = listBoxEmployees.SelectedItem;
var employee = this.employees.Single(c => (c.LastName + ", " + c.FirstName) == name.ToString());
bool isHourly = employee is HourlyEmployee;
bool isSalary = employee is SalariedEmployee;
if (isHourly)
{
HourlyEmployeeForm editForm = new HourlyEmployeeForm();
editForm.HourlyEmployee = hourlyEmployee;
if (editForm.ShowDialog() == DialogResult.OK)
{
// If OK was selected, retrieve the updated details
// and update the form
hourlyEmployee = editForm.HourlyEmployee;
PopulateListBox();
}
// Remove the dialog box from memory
editForm.Dispose();
}
else if (isSalary)
{
SalariedEmployeeForm editForm = new SalariedEmployeeForm();
editForm.ShowDialog();
}
}
#2. Posted:
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Status: Offline
Joined: Dec 30, 201211Year Member
Posts: 3,778
Reputation Power: 3016
Show your Load function within the employees class. it just seems like you're not closing the connection to the file as well.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.