Save And Load Data From Form

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

40 Replies - 1725 Views - Last Post: 08 May 2010 - 10:32 PM Rate Topic: -----

#1 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Save And Load Data From Form

Posted 05 May 2010 - 06:32 AM

I'm working on my final project and have just about everything done except getting the data in the listbox to save and load back up. As of now I have

            //create save file dialog
            SaveFileDialog fileChooser = new SaveFileDialog();

            
                try
                {
                    fileChooser.Filter = ("Text Document|*.txt|All Files|*.*");
                    fileChooser.ShowDialog();
                    fileWriter = new StreamWriter(fileChooser.FileName);
                    for (int I = 0; I < listBox1.Items.Count; I++)
                    {
                        fileWriter.WriteLine(Convert.ToString(listBox1.Items[I]));
                    }
                    fileWriter.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                } 
                
            }


what it is doing is saving the text in the listbox but not the rest of the data inputted. I've attached what I have done now so you could get a better feel/look on whats happening.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Save And Load Data From Form

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Save And Load Data From Form

Posted 05 May 2010 - 06:55 AM

what other data are you wanting it to save? The application is performing exactly what you told it to do.
Was This Post Helpful? 0
  • +
  • -

#3 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 05 May 2010 - 07:03 AM

View Posteclipsed4utoo, on 05 May 2010 - 05:55 AM, said:

what other data are you wanting it to save? The application is performing exactly what you told it to do.


I want it to save whats in the listbox and then when you load it back up and double click it the data is filled back into the form properties. Sorry if I was unclear.
Was This Post Helpful? 0
  • +
  • -

#4 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 05 May 2010 - 04:35 PM

any help?
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Save And Load Data From Form

Posted 06 May 2010 - 04:42 AM

here is a sample on how to read from a file...

string filePath = @"C:\test\test.txt";
List<string> lines = new List<string>();

using (TextReader tr = new StreamReader(filePath))
{
    while(tr.Peek() >= 0)
    {
         lines.Add(tr.ReadLine());
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 06 May 2010 - 06:44 AM

View Posteclipsed4utoo, on 06 May 2010 - 03:42 AM, said:

here is a sample on how to read from a file...

string filePath = @"C:\test\test.txt";
List<string> lines = new List<string>();

using (TextReader tr = new StreamReader(filePath))
{
    while(tr.Peek() >= 0)
    {
         lines.Add(tr.ReadLine());
    }
}



That's not what I needed.

The problem is if you run the program you click the add button fill out the info press ok, it appears in the listbox, if you double click the listbox a form will pop up with the info you put in. What I'm having problems with is if you click the save as in the file menu and save it then load the txt document and double click it in the listbox it doesnt load, that where I'm having the problem.

I added an updated project

Attached File(s)


This post has been edited by JaKha: 06 May 2010 - 01:22 PM

Was This Post Helpful? 1
  • +
  • -

#7 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 06 May 2010 - 01:12 PM

anyone have any ideas?
edit: sorry, the attached file is missing key parts I'll upload a fixed version soon.

edit: the project was updated in vs 2010 I can put a vs 2008 if needed.

Attached File(s)


This post has been edited by JaKha: 06 May 2010 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 08 May 2010 - 02:30 PM

Bump:

Problem 1: When the employee is saved and re-opened you cannot double click the name without the "Object reference not set to an instance of an object." error coming up.

SaveAs
  private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter Write;
            SaveFileDialog Open = new SaveFileDialog();
            try
            {
                Open.Filter = ("Text Document|*.txt|All Files|*.*");
                Open.ShowDialog();
                Write = new StreamWriter(Open.FileName);
                for (int I = 0; I < listBox1.Items.Count; I++)
                {
                    Write.WriteLine(Convert.ToString(listBox1.Items[I]));
                }
                Write.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }

           
        } 

Open
   private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 
        }

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#9 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Save And Load Data From Form

Posted 08 May 2010 - 02:48 PM

What is in the download file?

By the way i see that you are saving the items of the list, but what was that about double clicking the item?
Could we see that part of the code? Or maybe the hole code?
Have you checked if the text file is being saved correctly?
Was This Post Helpful? 0
  • +
  • -

#10 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 08 May 2010 - 02:54 PM

View Postponcho4all, on 08 May 2010 - 01:48 PM, said:

What is in the download file?

By the way i see that you are saving the items of the list, but what was that about double clicking the item?
Could we see that part of the code? Or maybe the hole code?
Have you checked if the text file is being saved correctly?


If by download file you mean the saved txt file, only the first name and last name are saved. If your talking about the attachment its an updated version of the project.

The double clicking is when you complete the employee form it will show the first name and last name on the list box. If you double click it, it will make the employee form pop back up with the employee's information in the text boxes.

 private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                Employee selectedEmp = listBox1.SelectedItem as Employee;
                FrmProperties myForm = new FrmProperties();
                //take info from the employee object
                //pass to properties form
                myForm.firstName = selectedEmp.FirstName;
                myForm.lastName = selectedEmp.LastName;
                myForm.empType = selectedEmp.EmpType;
                myForm.salary = selectedEmp.Salary;

                //show the form
                DialogResult result;
                result = myForm.ShowDialog();
                //check if they clicked cancel
                if (result == DialogResult.Cancel)
                    return;
                //if not cancel - get the info from the
                //public vars on properties form
                // give to employee object properties
                selectedEmp.FirstName = myForm.firstName;
                

                //remove this employee from listbox
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);

                //re-add employee object
                listBox1.Items.Add(selectedEmp);
            }
        } 



My guess is that the save file isn't being saved correctly and it's just saving whats in the listbox which is first name and last name instead of the form data.

Here is the whole form1.cs

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;
using System.IO;

namespace FinalProject
{
    public partial class Form1 : Form
    {
        StreamWriter fileWriter;
        FileStream output;
        StreamReader fileReader;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add();
        }

        public void Add()
        {
            //all code for add goes here
            FrmProperties myForm = new FrmProperties();
            DialogResult result;
            result = myForm.ShowDialog();
            
            if (result == DialogResult.Cancel)
                return;

            string fname = myForm.firstName;
            string lname = myForm.lastName;
            string emptype = myForm.empType;
            decimal money = myForm.salary;
            Employee myEmp = new Employee(myForm.firstName, myForm.lastName,
            myForm.empType, myForm.salary);

            listBox1.Items.Add(myEmp);
            myForm.Close();
        }
        public void Properties()
        {
            Employee selectedEmp = listBox1.SelectedItem as Employee;
                FrmProperties myForm = new FrmProperties();
                //take info from the employee object
                //pass to properties form
                myForm.firstName = selectedEmp.FirstName;
                myForm.lastName = selectedEmp.LastName;
                myForm.empType = selectedEmp.EmpType;
                myForm.salary = selectedEmp.Salary;

                //show the form
                DialogResult result;
                result = myForm.ShowDialog();
                //check if they clicked cancel
                if (result == DialogResult.Cancel)
                    return;
                //if not cancel - get the info from the
                //public vars on properties form
                // give to employee object properties
                selectedEmp.FirstName = myForm.firstName;
                

                //remove this employee from listbox
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);

                //re-add employee object
                listBox1.Items.Add(selectedEmp);
        }
        public void Delete()
        {
            while (listBox1.SelectedItems.Count > 0)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }
        public void SaveAs()
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 

        }

        public void Open()
        {
            StreamWriter Write;
            SaveFileDialog Open = new SaveFileDialog();
            try
            {
                Open.Filter = ("Text Document|*.txt|All Files|*.*");
                Open.ShowDialog();
                Write = new StreamWriter(Open.FileName);
                for (int I = 0; I < listBox1.Items.Count; I++)
                {
                    Write.WriteLine(Convert.ToString(listBox1.Items[I]));
                }
                Write.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                Properties();
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter Write;
            SaveFileDialog Open = new SaveFileDialog();
            try
            {
                Open.Filter = ("Text Document|*.txt|All Files|*.*");
                Open.ShowDialog();
                Write = new StreamWriter(Open.FileName);
                for (int I = 0; I < listBox1.Items.Count; I++)
                {
                    Write.WriteLine(Convert.ToString(listBox1.Items[I]));
                }
                Write.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }

           
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void propertiesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Employee selectedEmp = listBox1.SelectedItem as Employee;
            FrmProperties myForm = new FrmProperties();
            //take info from the employee object
            //pass to properties form
            myForm.firstName = selectedEmp.FirstName;
            myForm.lastName = selectedEmp.LastName;
            myForm.empType = selectedEmp.EmpType;
            myForm.salary = selectedEmp.Salary;

            //show the form
            DialogResult result;
            result = myForm.ShowDialog();
            //check if they clicked cancel
            if (result == DialogResult.Cancel)
                return;
            //if not cancel - get the info from the
            //public vars on properties form
            // give to employee object properties
            selectedEmp.FirstName = myForm.firstName;


            //remove this employee from listbox
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);

            //re-add employee object
            listBox1.Items.Add(selectedEmp);
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Employee Program by \r\nJacob Hannah (C) 2010.");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Save And Load Data From Form

Posted 08 May 2010 - 03:03 PM

Yea i meant the attachment. Is just that i downloaded it and there is nothing or i cant see nothing. It should me only Hex documentation for some reason.

Well ok, here is what i see, you are right, the information is not being saved correctly, by saving the items on the list box you are only saving the name. And on the program what you use on the list box is an employee class element, and the list box only directs you to it right?

Well if im right you will need to save not only what is on the list box but also all the information of what it think is an employee.

You could do that and as an idea for reading you could save it like, the first name on the list box and then all the information that the name has, for each of the elements that way when you read it you save the info of the employee on a employee variable and the name on the list box that way your program should work correctly.

Did I understood wrong?
Was This Post Helpful? 0
  • +
  • -

#12 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 08 May 2010 - 03:10 PM

View Postponcho4all, on 08 May 2010 - 02:03 PM, said:

Yea i meant the attachment. Is just that i downloaded it and there is nothing or i cant see nothing. It should me only Hex documentation for some reason.

Well ok, here is what i see, you are right, the information is not being saved correctly, by saving the items on the list box you are only saving the name. And on the program what you use on the list box is an employee class element, and the list box only directs you to it right?

Well if im right you will need to save not only what is on the list box but also all the information of what it think is an employee.

You could do that and as an idea for reading you could save it like, the first name on the list box and then all the information that the name has, for each of the elements that way when you read it you save the info of the employee on a employee variable and the name on the list box that way your program should work correctly.

Did I understood wrong?


I think your spot on with the problem, could you give me some sample or psuedo code to help out a little bit.
And try this attached zip and tell me if it works, I think the old zip was setup for Visual Studio 2010.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#13 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Save And Load Data From Form

Posted 08 May 2010 - 03:15 PM

Im not in my laptop so i dont have Visual Studio 2010 installed only 2008, but i still cant see the attachment.

Well i would have to check the code. Give me a while.

Is there an Employee class? Or any other class that is not posted?

Are you Zipping the whole file?

This post has been edited by poncho4all: 08 May 2010 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#14 JaKha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 13-April 10

Re: Save And Load Data From Form

Posted 08 May 2010 - 03:29 PM

View Postponcho4all, on 08 May 2010 - 02:15 PM, said:

Im not in my laptop so i dont have Visual Studio 2010 installed only 2008, but i still cant see the attachment.

Well i would have to check the code. Give me a while.

Is there an Employee class? Or any other class that is not posted?

Are you Zipping the whole file?

Here is the Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinalProject
{
    class Employee
    {
        //constructor
        public Employee(string firstname, string lastname, string type, decimal amount)
        {
            FirstName = firstname;
            LastName = lastname;
            EmpType = type;
            Salary = amount;
        }
        //private variables
        private string fname;
        private string lname;
        private string empType;
        private decimal salary;

        //public properties
        public string FirstName
        {
            get { return fname; }
            set { fname = value; }
        }
        public string LastName
        {
            get { return lname; }
            set { lname = value; }
        }
        public string EmpType
        {
            get { return empType; }
            set { empType = value; }
        }
        public decimal Salary
        {
            get { return salary; }
            set { salary = value; }
        }
        public override string ToString()
        {
            return LastName + ", " + FirstName;
        }
    }
}




Here is the entire form1.cs

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;
using System.IO;

namespace FinalProject
{
    public partial class Form1 : Form
    {
        StreamWriter fileWriter;
        FileStream output;
        StreamReader fileReader;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add();
        }

        public void Add()
        {
            //all code for add goes here
            FrmProperties myForm = new FrmProperties();
            DialogResult result;
            result = myForm.ShowDialog();
            
            if (result == DialogResult.Cancel)
                return;

            string fname = myForm.firstName;
            string lname = myForm.lastName;
            string emptype = myForm.empType;
            decimal money = myForm.salary;
            Employee myEmp = new Employee(myForm.firstName, myForm.lastName,
            myForm.empType, myForm.salary);

            listBox1.Items.Add(myEmp);
            myForm.Close();
        }
        public void Properties()
        {
            Employee selectedEmp = listBox1.SelectedItem as Employee;
                FrmProperties myForm = new FrmProperties();
                //take info from the employee object
                //pass to properties form
                myForm.firstName = selectedEmp.FirstName;
                myForm.lastName = selectedEmp.LastName;
                myForm.empType = selectedEmp.EmpType;
                myForm.salary = selectedEmp.Salary;

                //show the form
                DialogResult result;
                result = myForm.ShowDialog();
                //check if they clicked cancel
                if (result == DialogResult.Cancel)
                    return;
                //if not cancel - get the info from the
                //public vars on properties form
                // give to employee object properties
                selectedEmp.FirstName = myForm.firstName;
                

                //remove this employee from listbox
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);

                //re-add employee object
                listBox1.Items.Add(selectedEmp);
        }
        public void Delete()
        {
            while (listBox1.SelectedItems.Count > 0)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }
        public void SaveAs()
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 

        }

        public void Open()
        {
            StreamWriter Write;
            SaveFileDialog Open = new SaveFileDialog();
            try
            {
                Open.Filter = ("Text Document|*.txt|All Files|*.*");
                Open.ShowDialog();
                Write = new StreamWriter(Open.FileName);
                for (int I = 0; I < listBox1.Items.Count; I++)
                {
                    Write.WriteLine(Convert.ToString(listBox1.Items[I]));
                }
                Write.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                Properties();
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StreamWriter Write;
            SaveFileDialog Open = new SaveFileDialog();
            try
            {
                Open.Filter = ("Text Document|*.txt|All Files|*.*");
                Open.ShowDialog();
                Write = new StreamWriter(Open.FileName);
                for (int I = 0; I < listBox1.Items.Count; I++)
                {
                    Write.WriteLine(Convert.ToString(listBox1.Items[I]));
                }
                Write.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }

           
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void propertiesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Employee selectedEmp = listBox1.SelectedItem as Employee;
            FrmProperties myForm = new FrmProperties();
            //take info from the employee object
            //pass to properties form
            myForm.firstName = selectedEmp.FirstName;
            myForm.lastName = selectedEmp.LastName;
            myForm.empType = selectedEmp.EmpType;
            myForm.salary = selectedEmp.Salary;

            //show the form
            DialogResult result;
            result = myForm.ShowDialog();
            //check if they clicked cancel
            if (result == DialogResult.Cancel)
                return;
            //if not cancel - get the info from the
            //public vars on properties form
            // give to employee object properties
            selectedEmp.FirstName = myForm.firstName;


            //remove this employee from listbox
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);

            //re-add employee object
            listBox1.Items.Add(selectedEmp);
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Employee Program by \r\nJacob Hannah (C) 2010.");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            {
                this.listBox1.Items.Clear();
                OpenFileDialog Open = new OpenFileDialog();
                Open.Filter = "Text Document|*.txt|All Files|*.*";
                try
                {
                    Open.ShowDialog();
                    StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                    while (Import.Peek() >= 0)
                        listBox1.Items.Add(Convert.ToString(Import.ReadLine()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex.Message));
                    return;
                }
            } 
        }
    }
}



heres a 3rd attempt at an attachment lol.

Attached File(s)


This post has been edited by JaKha: 08 May 2010 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#15 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Save And Load Data From Form

Posted 08 May 2010 - 03:42 PM

You have a main form with a button add.
When you click this button you create a new form, that asks for the name and all the stuff of the employee, and then you save that employee on the list box.
What i cant imagine is what is shown on the list box of the main form when you do this
listBox1.Items.Add(myEmp);


Well what i would do is, go throw all the items on the list, then open each employee and save its characteristics. By save i mean writting all this on a text.
For loading i would read the file the same way i saved it, i mean:

Say there is one employee with fname and lname. And its part of a listbox

So
get the employee from listbox
then save fname on one line and lname on the next line on a text
then when you want to load it, you declare a variable employee, take the fname wich you now is the first line and then lname from the second line, and then add to list box. That way you have the attributes of the employee back.

This post has been edited by poncho4all: 08 May 2010 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3