I create a form that can add,edit and delete particular into the listbox. The problem now is that i wan to save the particular on the listbox and wen i go in back i can load it back. i hope anyone out there can help me.

This is the code:

public partial class Register : Form
{
    public Register()
    {
        InitializeComponent();
    }

    private void addButton_Click(object sender, EventArgs e)
    {
        EditDialog newEmployeeDialog = new EditDialog();
        if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
        {
            employeeList.Items.Add(newEmployeeDialog.StaffMember);
        }
    }

    private void editButton_Click(object sender, EventArgs e)
    {
        if (employeeList.SelectedIndex == -1)
            return;

        int employeeNum = employeeList.SelectedIndex;
        EditDialog newEmployeeDialog = new EditDialog();
        newEmployeeDialog.StaffMember =
            (Employee)employeeList.SelectedItem;

        if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
        {
            employeeList.Items.RemoveAt(employeeNum);
            employeeList.Items.Insert(employeeNum,
                    newEmployeeDialog.StaffMember);
            employeeList.SelectedIndex = employeeNum;
        }
    }

    private void deleteButton_Click(object sender, EventArgs e)
    {
        if (employeeList.SelectedIndex == -1)
            return;

        if (MessageBox.Show("Really delete this employee",
            "Delete", MessageBoxButtons.YesNo,
            MessageBoxIcon.Question)
            == DialogResult.Yes)
        {
            employeeList.Items.Remove(employeeList.SelectedItem);
        }

    }

    private void deleteButton_Click_1(object sender, EventArgs e)
    {
        if (employeeList.SelectedIndex == -1)
            return;

        if (MessageBox.Show("Remove this animal?",
            "Delete", MessageBoxButtons.YesNo,
            MessageBoxIcon.Question)
            == DialogResult.Yes)
        {
            employeeList.Items.Remove(employeeList.SelectedItem);
        }

    }

    private void employeeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (employeeList.SelectedIndex != -1)
        {

            Employee currentEmployee = (Employee)employeeList.SelectedItem;
            firstName.Text = currentEmployee.FirstName;
            lastName.Text = currentEmployee.LastName;
            jobTitle.Text = currentEmployee.JobTitle;
        }
        else
        {
            firstName.Text = lastName.Text = jobTitle.Text = "";
        }
    }

    private void backButton_Click(object sender, EventArgs e)
    {
        Menu f2 = new Menu();
        f2.Show(); // Opens the Menu
        this.Hide();// Closes the register
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
        what should i fill here so that i can save the data from the listbox and can reload back the data.
    }
    }
    }

Edited 3 Years Ago by pritaeas: Fixed formatting

LizR is right you can use a serialization to persist objects in your hard drive, I can show you how to do exactly

As I know you have a set of employee objects to store,

you can do this:

first create an emplyee class

public class employee
{
public employee() {}
.......
/*The rest of implementation*/


}


then add System.xml.serialization and system.IO to the the namespaces list

then write this code

streamWriter oWriter = new streamWriter(put your path here of an xml file);

xmlserilizer oSerialiser = new xmlSerilizer(typeof(Employee));
oSerilizer.Serilize(oWriter,an instance of employee here);
oWriter.Flush();
oWriter.Close();

if you want to get your employee Then use a xmlreader instead of xmlwriter and use this method to retrieve the object

Employee oEmployee = oSerilizer.Decerialize(....) as Employee;

There are two other ways to serialize objects using bianry serilizer and soap serilizer

i dun understand. cause i stil new to this program. can help me do that part? i send u the file. cause my date due in 2 weeks tyme, pls help me!

But you've had example code - what exactly dont you understand - and if its for your homework - I would like to bet you've already been shown ways you could do it..

this is my final year project. i never learn it before but i have to learn. most of the code i learnt form books and google searching. the problem is tat i duno the commad for save and load..? do i have to declare.. i been tryin searching google where mosstly save frm textbox not listbox. dey have the answer but i cant understd. could u help with these code?

Well, firstly the keywords I gave you should have been enough to read through the microsoft examples showing you exactly how to make it work - so not understanding shouldnt be an issue

Secondly, Jugortha even posted you the code? For which you havent tried.. So.

I ask again, what exactly dont you understand?

first create an emplyee class<- is this mean i add a new class?

public class employee
{
public employee() {}
....... <-- what should i add?
/*The rest of implementation*/


}


then add System.xml.serialization and system.IO to the the namespaces list <-- what this mean and where should i put the code?

then write this code

streamWriter oWriter = new streamWriter(put your path here of an xml file);<-- is this code put at my save button code? xml is what?

xmlserilizer oSerialiser = new xmlSerilizer(typeof(Employee));
oSerilizer.Serilize(oWriter,an instance of employee here);
oWriter.Flush();
oWriter.Close();

if you want to get your employee Then use a xmlreader instead of xmlwriter and use this method to retrieve the object

Employee oEmployee = oSerilizer.Decerialize(....) as Employee; <-- what i type for load? can giv the full code for load?


btw thks for helping.. i appreciate alot!

I am astounded.

if you're doing this and you havent *any* ideas to those questions you should really consider going and telling your professor you either havent listened or your on the wrong course!

You also should read the stickies we *WONT* do your work for you. You need to do that - either that or you give use your qualification at the end.

If you had looked at the keywords you'd know it doesnt have to be XML - but it is one of the options. If you dont know what XML is - try google, its a tad popular.

As for the code.

Look at the code, and what he wrote, what do you *think* it might do?

ok. i know that u will be shock. i now go learn frm basic.. btw thks for helping

new or not, I seriously doubt theres any lectures whod set a task like that who hasnt showed them ways to do it, or at least suggested reading material..

This article has been dead for over six months. Start a new discussion instead.