1

I'm working with ListBoxes in WPF and am trying to save items from different list boxes into the same file. As for now each listbox gets their own file.

This is my code so far:

private void OnSaveAs()
{
    SaveFileDialog save = new SaveFileDialog();
    save.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

    if (save.ShowDialog() == true)
    {
        //File.WriteAllText(save.FileName, InputsMinMax.Text);
    }


    if (this.InputsMV.Items.Count > 0 && save.ShowDialog() == true)
    {
        using (FileStream S = File.Open(save.FileName, FileMode.CreateNew))
        {
            using (StreamWriter st = new StreamWriter(S))
            {
                foreach (var aa in InputsMV.Items)
                    st.WriteLine(aa.ToString());
            }
        }

    }
}
Krzysztof Bracha
  • 903
  • 7
  • 19
coder
  • 538
  • 7
  • 17
  • Please check https://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish. You wont face this if you go for `MVVM`. – Peter Jun 28 '17 at 10:57

1 Answers1

1

To save items from multiple ListBox elements you can create a simple file format where for each element you save its name, number of items and item values:

[line 0]     ListBox name
[line 1]     Number of items - n
[line 2]     Item 1
[line 3]     Item 2
 ...         ...     
[line n + 1] Item n 
[line n + 2] Next ListBox name 

Note that this format assumes that items do not contain new line characters.

Then you can save items from all elements like this:

private void OnSaveAs()
{
    SaveFileDialog save = new SaveFileDialog();
    save.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

    if (save.ShowDialog() == true)
    {
        using (FileStream S = File.Open(save.FileName, FileMode.CreateNew))
        {
            using (StreamWriter st = new StreamWriter(S))
            {
                // Iterate over ListBox elements
                foreach (var myListBox in MyListBoxes)
                {
                    // Write the name of the ListBox element
                    streamWriter.WriteLine(myListBox.Name);

                    // Write the number of elements
                    streamWriter.WriteLine(myListBox.Items.Count);

                    // Write the elements
                    foreach (var item in myListBox.Items)
                    {
                        streamWriter.WriteLine(item.ToString());
                    }
                }
            }
        }
    }
}
Krzysztof Bracha
  • 903
  • 7
  • 19
  • Is "MyListBoxes" a list where I put all my boxes? – coder Jun 28 '17 at 11:36
  • @Anna Yes, you can collect them in a list. – Krzysztof Bracha Jun 28 '17 at 11:42
  • Hm.. @Krzysztof. I saved them in a list like: List listBoxes = new List(); and then tried to iterate over the listbox elements like you told me: foreach(var myListBox in listBoxes) but it doesn't work. When I open the text file it's empty even though I have items in my list boxes. – coder Jun 28 '17 at 11:59
  • It works now! I think I used your "old" version. I saw now that you had edited it. Thank you so much! Do you know if it is possible to make the name of the ListBox bold? Tried googling it but didn't find anything. – coder Jun 28 '17 at 12:11
  • @Anna Glad to help. I am not sure if I understand what do you mean by "make the name of the ListBox bold", could you please clarify? – Krzysztof Bracha Jun 28 '17 at 14:32
  • In every iteration it will write the name of the different ListBox elements, I would like the name to be bold, for example: **Listbox 1** @KrzysztofBracha – coder Jun 29 '17 at 05:35
  • @Anna Plain text files (.txt) consist only of characters and you cannot apply formatting to them. For the names to be able to appear bold you would need first to save them in a specific format (e.g. docx, html), add formatting, and then open them in a specific application (e.g. Word, web browser). – Krzysztof Bracha Jun 29 '17 at 07:38
  • Oh I see! Thank you! @KrzysztofBracha – coder Jun 29 '17 at 12:29