I have a listbox titled lstResult1 and lstResult2. I am pretty new to C# programming. I am trying to save the two list boxes along with the rest of the file. I was able to save the three textboxes to the file but i cant get the two listboxes to save to the file. lstResult1 contains names (string) and lstResult2 contains (double) values. Any help would be appreciated
Here is my code. i dont know why but im trying to do the second listbox first. but i do need help with both.

private void mnuFileSave_Click(object sender, EventArgs e)
         private void mnuFileSave_Click(object sender, EventArgs e)
            {
            if (dlgSave.ShowDialog() == DialogResult.OK)
            {
               
                FileStream fileAndorHunsFormulaWorksheet = new FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write, FileShare.Write);
                BinaryWriter bnrAndorHunsFormulaWorksheet = new BinaryWriter(fileAndorHunsFormulaWorksheet);
                bnrAndorHunsFormulaWorksheet.Write(txtFormulaName.Text);
                bnrAndorHunsFormulaWorksheet.Write(txtFormulaNumber.Text);
                bnrAndorHunsFormulaWorksheet.Write(txtItemName.Text);
                
                
                foreach (double value in lstResult2.Items)

                {
                    
                    
                    bnrAndorHunsFormulaWorksheet.Write(Convert.ToString(lstResult2.Items));
                    bnrAndorHunsFormulaWorksheet.Flush();
                }
                
                bnrAndorHunsFormulaWorksheet.Close();
                fileAndorHunsFormulaWorksheet.Close();
kvprajapati commented: ++repulation. 1st post with [code] tags. +11

You can do this by making a loop to the list of the items in the listbox. And for each loop you can save the data by taking listitem[0].text


bnrAndorHunsFormulaWorksheet.Write(listitem[0].text);

Munna(i's).

Refer this

protected void Button1_Click(object sender, EventArgs e)
        {
            FileStream fileAndorHunsFormulaWorksheet = new FileStream("C:\\Users\\ravikiran.k\\Desktop\\tt.txt", FileMode.Create, FileAccess.Write, FileShare.Write);
            BinaryWriter bnr = new BinaryWriter(fileAndorHunsFormulaWorksheet);
            bnr.Write(TextBox1.Text);
            bnr.Write(TextBox2.Text);
            foreach (ListItem ls in ListBox1.Items)
            {
                bnr.Write(ls.Text);
            }
            fileAndorHunsFormulaWorksheet.Close();
        }

Munna(i's).

ok thanks for the reply. when i put ListItem in the the foreach loop it says The type or namespace name 'ListItem' could not be found (are you missing a using directive or an assembly reference?) I dont know how to fix this. btw the file extension is .pfw.

You can change this:

foreach (ListItem ls in ListBox1.Items)
{
bnr.Write(ls.Text);
}

With this as well:

foreach (object ls in ListBox1.Items)
            {
                bnr.Write(ls.ToString());
            }

Ok so this is what i have now. i put in what you guys said and i cant get it to open the file and display the items in the list box. this is the code i have.

private void mnuFileOpen_Click(object sender, EventArgs e)
        {
            dlgOpen.InitialDirectory = @"C:\";

            if (dlgOpen.ShowDialog() == DialogResult.OK)
            {
                FileStream fileAndorHunsFormulaWorksheet = new FileStream(this.dlgOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader bnrAndorHunsFormulaWorksheet = new BinaryReader(fileAndorHunsFormulaWorksheet);
                txtFormulaName.Text = bnrAndorHunsFormulaWorksheet.ReadString();
                txtFormulaNumber.Text = bnrAndorHunsFormulaWorksheet.ReadString();
                int number = lstResult2.Items.Count;

                foreach (string value in lstResult2.Items)
                {
                    lstResult2.Items.Add(bnrAndorHunsFormulaWorksheet.ReadString());
                }
                bnrAndorHunsFormulaWorksheet.Close();
                fileAndorHunsFormulaWorksheet.Close();
            }

        }

        private void mnuFileSave_Click(object sender, EventArgs e)
        {
            if (dlgSave.ShowDialog() == DialogResult.OK)
            {
               
                FileStream fileAndorHunsFormulaWorksheet = new FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write, FileShare.Write);
                BinaryWriter bnrAndorHunsFormulaWorksheet = new BinaryWriter(fileAndorHunsFormulaWorksheet);
                bnrAndorHunsFormulaWorksheet.Write(txtFormulaName.Text);
                bnrAndorHunsFormulaWorksheet.Write(txtFormulaNumber.Text);
                bnrAndorHunsFormulaWorksheet.Write(txtItemName.Text);


                foreach (object ls in lstResult2.Items)
                {
                    bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
                }
                bnrAndorHunsFormulaWorksheet.Close();
                fileAndorHunsFormulaWorksheet.Close();

i dont know if its the code for saving or the code for opening thats giving me the problem. any additional help would be appreciated.

why don't you just serialize the list objects? it sounds like thats what your trying to do anyway

try googling csharp object serialization

i just want to know whats wrong with the code that i have. i dont want to add more code if the mistake is already in the code that i wrote

There is a problem while reading a stream.

...

if (dlgOpen.ShowDialog() == DialogResult.OK)
            {
                FileStream fileAndorHunsFormulaWorksheet = new FileStream(this.dlgOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryReader bnrAndorHunsFormulaWorksheet = new BinaryReader(fileAndorHunsFormulaWorksheet);
                txtFormulaName.Text = bnrAndorHunsFormulaWorksheet.ReadString();
                txtFormulaNumber.Text = bnrAndorHunsFormulaWorksheet.ReadString();
                txtItemName.Text = bnrAndorHunsFormulaWorksheet.ReadString();
                int number = lstResult2.Items.Count;
                
                lstResult2.Items.Clear();

                while(bnrAndorHunsFormulaWorksheet.PeekChar()!=-1)
                {
                    lstResult2.Items.Add(bnrAndorHunsFormulaWorksheet.ReadString());
                }
                bnrAndorHunsFormulaWorksheet.Close();
                fileAndorHunsFormulaWorksheet.Close();
            }
 ...

ok thanks so much. i have one more question. I have two listboxes. The one above (lstResult2) just has numbers in it. how do you load the results for the other list box and have the name come out in lstResult and the value in lstResult2. lstResult contains Names.

Write

.....
               foreach (object ls in lstResult2.Items)
                {
                    bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
                }
                bnrAndorHunsFormulaWorksheet.Write("@"); // Marker

                foreach (object ls in lstResult.Items)
                {
                    bnrAndorHunsFormulaWorksheet.Write(ls.ToString());
                }
                
..

Read

...
                lstResult2.Items.Clear();
                lstResult.Items.Clear();

                bool switchAction = false;

                while (bnrAndorHunsFormulaWorksheet.PeekChar() != -1)
                {
                    string str = bnrAndorHunsFormulaWorksheet.ReadString();
                    if (str == "@")
                    {
                        switchAction = true;
                        continue;
                    }
                    if (switchAction)
                        lstResult.Items.Add(str);
                    else
                        lstResult2.Items.Add (str);
                } 
...

In foreach you are using string instead of ListItem that the main prob in ur code.
And
Instead of

lstResult2.Items.Add(bnrAndorHunsFormulaWorksheet.ReadString());

Use bnr.Write(ls.Text); Its very simple and u r making it as skycatcher. refer Post 3

Thanks so much for all of your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.