Writing List items to text file c# - c#

I am trying to get a List to save into a text file and am running into a problem.
It will save into the text file but not all the information as needed but only the information that actually shows in the ListBox. Suggestions?
namespace Employee_Form
{
public partial class frmMain : Form
{
FileStream output;
StreamReader fileReader;
//StreamWriter fileWriter;
List<Employee> employeeList = new List<Employee>();
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFile();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveAs();
}
private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
{
PropertiesOpen();
}
private void PropertiesOpen()
{
//creates an instance of the Properties Form
frmProperties myform = new frmProperties();
DialogResult result = myform.ShowDialog();
}
//Opens a file chosen by a user and places information into the listbox
private void OpenFile()
{
OpenFileDialog fileChooser = new OpenFileDialog();
fileChooser.Title = "Pick a file";
fileChooser.Filter = "Text Files (*.txt) | *.txt";
DialogResult result = fileChooser.ShowDialog();
//
if (result == DialogResult.Cancel)
{
//do nothing
return;
}
string strFileName = fileChooser.FileName;
try
{
//open the file for read access
output = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
fileReader = new StreamReader(output);
//variables to hold read record
string strInputLine;
string[] fields;
//loop to get records and break into fields
while (fileReader.EndOfStream != true)
{
//read record
strInputLine = fileReader.ReadLine();
//split the records when read
fields = strInputLine.Split(',');
//add records to the list box
employeeList.Add(new Employee(fields[1], fields[0], fields[2],
Convert.ToDouble(fields[3])));
}
lstRecords.DataSource = employeeList;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//closes fileReader and output to save Resources
fileReader.Close();
output.Close();
}
}
public void SaveAs()
{
//create a file dialog
SaveFileDialog fileChooser = new SaveFileDialog();
fileChooser.Title = "Choose A Save Location";
fileChooser.Filter = "Text Files (*txt)|*.txt";
//open the dialog and get a result
DialogResult result = fileChooser.ShowDialog();
//checks if user clicks cancel
if (result == DialogResult.Cancel)
{
return;
}
//get the file name from the dialog
string strFileName = fileChooser.FileName;
try
{
//open the new file for write access
StreamWriter SaveFile = new StreamWriter(strFileName);
foreach (var item in employeeList)
{
SaveFile.WriteLine(item.ToString());
}
SaveFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//close resources
//fileWriter.Close();
output.Close();
}
}
}
Sorry, I am new to this. There are two forms and the second one is for editing/adding new employees. only need to show the first and last name in the ListBox. Here is my Employee class also:
public class Employee
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string EmpType
{
get;
set;
}
public double Salary
{
get;
set;
}
public Employee(string firstName, string lastName, string empType, double salary)
{
FirstName = firstName;
LastName = lastName;
EmpType = empType;
Salary = salary;
}
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}

When you call SaveFile.WriteLine(item.ToString());, you're writing the result of the ToString() method of Employee:
return string.Format("{0}, {1}", LastName, FirstName);
This is the same method called by the ListBox to display the object in the list. So the behavior you're seeing is exactly what one would expect.
If you want to see something different, try something like this:
SaveFile.WriteLine(string.Format("{0}, {1}, {2}, {3}", item.LastName, item.FirstName, item.EmpType, item.Salary));
Use whatever properties and formatting you want in your file.

Related

How do I read a file into a list?

I have a project to create a phone book of sorts. It needs to read a file and output the name to a list box in the main form. When the user selects a name a new form should open with the name phone number and email address.
Currently there are no syntax errors, but the program does not read the file. It always throws an exception. The file name is on my desktop and I verified the file name matches the code. Any insight on why this is happening would be appreciated.
Here is my code:
public partial class Form1 : Form
{
List<PersonEntry> nameList = new List<PersonEntry>();
public Form1()
{
InitializeComponent();
}
class PersonEntry
{
public string _name { get; set; }
public string _number { get; set; }
public string _email { get; set; }
}
private void GetNamesButton_Click(object sender, EventArgs e)
{
Readfile();
DisplayNameList();
}
private void Readfile()
{
try
{
StreamReader inputFile;
string line;
char[] deliminator = { ';' };
inputFile = File.OpenText("Personlist.txt");
while (!inputFile.EndOfStream)
{
//Use class
PersonEntry entry = new PersonEntry();
line = inputFile.ReadLine();
string[] tokens = line.Split(deliminator);
entry._name = tokens[0];
entry._number = tokens[1];
entry._email = tokens[2];
nameList.Add(entry);
}
}
catch
{
MessageBox.Show("Unable to open file");
}
}
private void DisplayNameList()
{
//Add the entry objects to the List
foreach (PersonEntry nameDisplay in nameList)
{
namesListBox.Items.Add(nameDisplay._name);
}
}
public void namesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (namesListBox.SelectedIndex != -1)
{
//Get full info for the selected item in the list
string name = nameList[namesListBox.SelectedIndex]._name;
string email = nameList[namesListBox.SelectedIndex]._email;
string phone = nameList[namesListBox.SelectedIndex]._number;
//Create second form for these details
DetailForm f2 = new DetailForm(name, email, phone);
f2.ShowDialog();
}
else
{
MessageBox.Show("Please select a Name.");
}
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
File is not in correct folder, exception shows correct file location. After moving it the application works as intended.

How do I join the selected items in multiple list boxes with the user text entered in text box in c#?

Goal:Is to display Project name, formed by joining list box selected items with user entered text in the text box and display it as a label.
Description: I am creating a windows for App, which has two listboxes displaying Firstname-lbA & Lastname- lbB along with a textbox for the user to enter packet name.
For example: If I select an item(first name)from the listbox A i.e. "XXX", select an item(last name)from the listbox B i.e. "YYY" and enter text in the Textbox i.e. "PKT-100" & click create button I would want to display the Project name as XXX-YYY-PKT-100.
checking conditions:
If no Item in lbA is selected then display:YYY-PKT-100.
If no Item in Lb B is seleected then display :XXX-PKT-100.
If no Text is entered the display: XXX-YYY.
No spaces/double dashes are allowed.
I'd really appreciate if someone could help me with your suggestions.
Thanks!!
Code:
FirstNamespace Project
{
public partial class ProjectTool : Form
{
public List<FirstName> ltfirstname { get; set; }
public List<LastName> ltlastname { get; set; }
public FirstName SelectedFirstname => (FirstName)lbGetFirstName.SelectedItem
public LastName SelectedLastname => (LastName)lbGetLastName.SelectedItem;
public projecTool()
{
InitializeComponent();
ltfirstname = GetFirstNames();
lbGetFirstName.DataSource = ltfirstname;
ltlastname = GetLastNames();
lbGetLastName.DataSource = ltlastname;
}
public List<FirstName> GetFirstNames()
{
List<FirstName> fnames = new List<FirstName>();
using (StreamReader sr = new StreamReader("D:\\FirstNames.csv"))
{
string line;
try
{
while ((line = sr.ReadLine()) != null)
{
string[] columns = line.Replace("\"","").Split(',');
if (columns.Length >= 1)
{
var name = new FirstName();
name.FirstName = columns[0];
fnames.Add(name);
}
}
}
catch (Exception ex) { }
return fnames;
}
}
public List<LastName> GetlastNames()
{
List<LastName> lnames = new List<LastName>();
using (StreamReader sr = new StreamReader("D:\\LastNames.csv"))
{
string line;
try
{
while ((line = sr.ReadLine()) != null)
{
string[] columns = line.Replace("\"","").Split(',');
if (columns.Length >= 1)
{
var lname = new LastName();
lname.LastName = columns[0];
lnames.Add(lname);
}
}
}
catch (Exception ex) { }
return lnames;
}
}
public void CreateprojectName()
{
// Create project name as Firstname-Lastname-Packetname
//if project name already exists then check for correctness/update.
// Check -> if there are double dashes/spaces in the Project name
}
private void lbGetFirstName_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void lbGetlastName_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void txtPacketName_TextChanged(object sender, EventArgs e)
{
}
private void create_projectname_button1_Click(object sender, EventArgs e)
{
}
}
}
Its quite simple for listBox use SelectedItem & for textBox use Text property.
private void create_projectname_button1_Click(object sender, EventArgs e)
{
if (lbGetFirstName.SelectedItems.Count==0)
{
label1.Text = "YYY-PKT-100";
}
if (lbGetlastName.SelectedItems.Count==0)
{
label1.Text = "XXX-PKT-100";
}
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
label1.Text = "XXX-YYY";
}
else
{
label1.Text = lbGetFirstName.SelectedItem + "-" + listBox2.SelectedItem + "-" + lbGetlastName.Text;
}
}

c# getting the right form to show

I have two forms a salaried employee and an hourly employee which has the employees details on it loading from a textfile. In my main form there is a listbox with the names of the employees, once one is clicked, i want to be able to press an edit employee details button on my main form and for the correct form to come up and I am struggling on how to do this. The code for my main form is here:
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;
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();
foreach (Employee employee in employees)
{
listBoxEmployees.Items.Add(employee.LastName + "," +
employee.FirstName);
}
listBoxEmployees.SelectedIndex = 0;
}
private void listBoxEmployees_DoubleClick(object sender, EventArgs e)
{ }
private void buttonEdit_Click(object sender, EventArgs e)
{
}
my load method:
{
public bool Load(string employeesFile)
{
bool status = false;
StreamReader inputFile = null;
string inputLine;
if (!File.Exists(employeesFile))
{
return false;
}
try
{
inputFile = new StreamReader(employeesFile);
if (inputFile != null)
{
inputLine = inputFile.ReadLine();
while (inputLine != null)
{
Employee employeeEntry =
EmployeeClass.NewEmployee(inputLine);
if (employeeEntry != null)
{
this.Add(employeeEntry);
}
inputLine = inputFile.ReadLine();
}
inputFile.Close();
}
status = true;
}
catch
{
}
return status;
}
}
the employees class code from the load method:
public class EmployeeClass
{
public static Employee NewEmployee(string employeeData)
{
if (employeeData.Length < 1)
{
return null;
}
switch (employeeData[0])
{
case 'S':
return new SalariedEmployee(employeeData);
case 'H':
return new HourlyEmployee(employeeData);
default:
return null;
the hourly employee form:
public partial class Hourly_Employee : Form {
HourlyEmployee _employeeEntry;
public Hourly_Employee()
{
InitializeComponent();
}
public HourlyEmployee employeeEntry
{
get
{
return _employeeEntry;
}
set
{
_employeeEntry = value;
}
}
private void Hourly_Employee_Load(object sender, EventArgs e)
{
textBoxlastName.Text = _employeeEntry.LastName;
textBoxfirstName.Text = _employeeEntry.FirstName;
textBoxaddress.Text = _employeeEntry.Address;
textBoxpostCode.Text = _employeeEntry.PostCode;
textBoxphoneNumber.Text = _employeeEntry.PhoneNumber;
dateTimePickerdateOfBirth.Text =
_employeeEntry.DateOfBirth.ToString();
textBoxhourlyPay.Text = _employeeEntry.HourlyPay.ToString();
textBoxoverTimePay.Text = _employeeEntry.OvertimePay.ToString();
}
}
and lastly my salaried employee form:
public partial class Salary_Employee : Form
{
SalariedEmployee _employeeEntry;
public SalariedEmployee employeeEntry
{
get
{
return _employeeEntry;
}
set
{
_employeeEntry = value;
}
}
private void Salary_Employee_Load(object sender, EventArgs e)
{
textBoxlastName.Text = _employeeEntry.LastName;
textBoxfirstName.Text = _employeeEntry.FirstName;
textBoxaddress.Text = _employeeEntry.Address;
textBoxpostCode.Text = _employeeEntry.PostCode;
textBoxphoneNumber.Text = _employeeEntry.PhoneNumber;
dateTimePickerdateOfBirth.Text =
_employeeEntry.DateOfBirth.ToString();
textBoxSalary.Text = _employeeEntry.Salary.ToString();
}
any help with this issue would be great!!
The trick is to add the employee objects to the listbox instead of only strings containing the employee names. This allows you to retrieve the selected employee directly from the listbox. Otherwise you would need a way to find the employee object belonging to a name.
Use the type of the selected item to determine the employee type and the employee form.
private void buttonEdit_Click(object sender, EventArgs e)
{
// Get the selected employee from the listBox.
object employee = listBoxEmployees.SelectedItem;
if (employee != null) { // An employee has been selected.
// Use the new C# 7.0 switch syntax in order to switch by employee type.
switch (employee) {
case SalariedEmployee sEmployee:
var sfrm = new Salary_Employee(); // Open the salaried employee form..
sfrm.employeeEntry = sEmployee;
sfrm.Show();
break;
case HourlyEmployee hEmployee:
var hfrm = new Hourly_Employee(); // Open the hourly employee form.
hfrm.employeeEntry = hEmployee;
hfrm.Show();
break;
}
}
}
In case you are using an older C# version you can test a type with
if (employee is SalariedEmployee) {
var frm = new Salary_Employee();;
frm.employeeEntry = (SalariedEmployee)employee;
frm.Show();
} else if (employee is HourlyEmployee) {
var frm = new Hourly_Employee();
frm.employeeEntry = (HourlyEmployee)employee;
frm.Show();
}
By default, an object's ToString method returns the name of the object's type. Enable the listbox to display the employees correctly by overriding the ToString method inherited from object in the common base class Employee.
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return LastName + ", " + FirstName;
}
}

Saving keeps overwriting itself C#

I am making an application which will save and load products. These products have three properties of a product name, customer name and firmware location. However, when I try to save them, it will only save one and keeps overwriting with the most recent product saved. The following is my code for the product class:
public class Product
{
//private product data
private string productName;
public string getProductName()
{
return this.productName;
}
public void setProductName (string inProductName)
{
this.productName = inProductName;
}
private string customerName;
public string getCustomerName()
{
return this.customerName;
}
public void setCustomerName (string inCustomerName)
{
this.customerName = inCustomerName;
}
private string firmwareLocation;
public string getFirmwareLocation()
{
return this.firmwareLocation;
}
public void setFirmwareLocation (string inFirmwareLocation)
{
this.firmwareLocation = inFirmwareLocation;
}
//constructor
public Product (string inProductName, string inCustomerName, string inFirmwareLocation)
{
productName = inProductName;
customerName = inCustomerName;
firmwareLocation = inFirmwareLocation;
}
//save method
public void Save (System.IO.TextWriter textOut)
{
textOut.WriteLine(productName);
textOut.WriteLine(customerName);
textOut.WriteLine(firmwareLocation);
}
public bool Save (string filename)
{
System.IO.TextWriter textOut = null;
try
{
textOut = new System.IO.StreamWriter(filename);
Save(textOut);
}
catch
{
return false;
}
finally
{
if (textOut != null)
{
textOut.Close();
}
}
return true;
}
At the end is my save methods.
Here is the code for when the user presses the add product button:
private void Add_Click(object sender, RoutedEventArgs e)
{
//get input from user
string inputCustomerName = customerNameTextBox.Text;
string inputProductName = productNameTextBox.Text;
string inputFirmwareLocation = firmwareTextBox.Text;
try
{
Product newProduct = new Product(inputProductName, inputCustomerName, inputFirmwareLocation);
newProduct.Save("products.txt");
MessageBox.Show("Product added");
}
catch
{
MessageBox.Show("Product could not be added");
}
}
You are not appending the text to your file, thats why it keeps overwriting the last entry over and over again.
Try to change your save method to:
public bool Save (string filename)
{
System.IO.TextWriter textOut = null;
try
{
textOut = new System.IO.StreamWriter(filename, true);
Save(textOut);
}
catch
{
return false;
}
finally
{
if (textOut != null)
{
textOut.Close();
}
}
return true;
}
Notice the "true" as the second parameter in the StreamWriter constructor. This tells the StreamWriter to append the new line.

File load and adding to list

I'm basically trying to add the items from a .txt file into a listbox. The problem is that the method for loading the .txt file is in a seperate class, so I came to a point where i don`t know what to do. Any help would be greatly appreciated. Here is the class, with the file loading method:
public class BunchOfDeliverables
{
private List<Person> myPersons;
private List<Deliverable> myDeliverables;
public BunchOfDeliverables()
{
this.myPersons = new List<Person>();
this.myDeliverables = new List<Deliverable>();
}
public List<Person> Persons { get { return this.myPersons; } }
public List<Deliverable> Deliverables { get { return this.myDeliverables; } }
public void LoadPersonsFromFile(String filename)
{
StreamReader sr = null;
try
{
sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
String name, street, housenr, postalcode, city;
name = sr.ReadLine();
while (name != null)
{
street = sr.ReadLine();
housenr = sr.ReadLine();
postalcode = sr.ReadLine();
city = sr.ReadLine();
this.myPersons.Add(new Person(name, street, Convert.ToInt32(housenr), postalcode, city));
name = sr.ReadLine();
name = sr.ReadLine(); //and again read a line, because of the delimiter (line with the stars)
}
}
catch (IOException) { }
finally
{
if (sr != null) sr.Close();
}
}
public void LoadDeliverablesFromFile(String filename)
{
StreamReader sr = null;
try
{
sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read));
String s;
s = sr.ReadLine();
while (s != null)
{
String[] items = s.Split();
this.myDeliverables.Add(new Deliverable(Convert.ToInt32(items[0]), Convert.ToInt32(items[1]), this.myPersons[Convert.ToInt32(items[2])]));
s = sr.ReadLine();
}
}
catch (IOException) { }
finally
{
if (sr != null) sr.Close();
}
}
public void AddPerson(Person p)
{
this.myPersons.Add(p);
}
public Deliverable FindDeliverable(int id)
{
foreach (Deliverable d in this.myDeliverables)
{
if (d.ID == id)
{
return d;
}
}
return null;
}
public void AddDeliverable(Deliverable d)
{
if (FindDeliverable(d.ID) == null)
{
myDeliverables.Add(d);
}
else
{
throw new Exception("Be aware: nothing is added!!!");
}
}
AND then here is the form class (mostly empty):
public partial class Form1 : Form
{
BunchOfDeliverables d;
public Form1()
{
InitializeComponent();
d = new BunchOfDeliverables();
}
private void AddLoadedFilesToListbox_Click(object sender, EventArgs e)
{
}
Edit :
I tryed the following but again it does not work:
public partial class Form1 : Form
{
BunchOfDeliverables d;
public Form1()
{
InitializeComponent();
d = new BunchOfDeliverables();
d.LoadDeliverablesFromFile("..data/deliverables.txt");
}
private void button1_Click(object sender, EventArgs e)
{
foreach (Deliverable deliv in d.Deliverables)
{
listBox1.Items.Add(deliv);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
It is that simple:
BunchOfDeliverables bunchOfDeliverables = new BunchOfDeliverables();
bunchOfDeliverables.LoadPersonsFromFile(personsFile);
bunchOfDeliverables.LoadDeliverablesFromFile(deliverablesFile);
listBox.DataSource = bunchOfDeliverables.Persons;
listBox.DisplayMember = "<Whatever>";
listBox.ValueMember = "<Whatever>";
// OR
listBox.DataSource = bunchOfDeliverables.Deliverables;
listBox.DisplayMember = "<Whatever>";
listBox.ValueMember = "<Whatever>";
I don't know which list (Persons or Deliverables) do you use, so the code contains both.

Categories

Resources