none
Saving multiple list box lists in one file? RRS feed

  • Question

  • Hey, 

    Im wondering if it is possible for me to save several listbox's into a text file, and then on loads, load them back into the appropriate listbox's.

    I have set up one save / load option. Now i have added more "log" data to my app, im wondering if the save button can save the data from a total of 4 listboxs and then on loads put the correct data back into each box?

    Thank a lot! :) 

    Monday, October 20, 2014 2:10 PM

Answers

  • Thanks guys.

    i think a few days away from it and coming back with a fresh mind made this a whole lot easier haha! 

    i simply made each line start with a different symbol, so text from the lstlog was saved starting with an "@" and data from the other boxes saved starting with a % & and #. 

    Then i just said while the line is not blank, if it contains a @, lstLog.Items.Add(line) else if it contains a % and so on!

    thanks again :) 



    Thursday, October 23, 2014 1:04 PM

All replies

  • Im wondering if it is possible for me to save several listbox's into a text file, and then on loads, load them back into the appropriate listbox's.


    Sure, it's possible. You just need to think it through.

    If you just write the data from the listboxes to the file, how are you going to tell where
    the data for a box starts and ends when you read the file back? What if one (or more) of
    the boxes is empty when the save is performed?

    One approach is to always write a control record as the first line in the file. It should
    contain a line count for each listbox being saved, including zero for any box that was
    empty when the save was done. Use those counts to load the correct number of lines from
    the file to each listbox.

    - Wayne


    Monday, October 20, 2014 4:04 PM
  • Using a textfile is real outdated from 1985. They invented then the INI file but that is currently only from some years later. 

    One of the approaches is an XML file. Be aware with overwriting it first set an old version of that in a temp and delete that and the end of the save.

    Wide description and samples on this page for the read, if you look then at the right you see a link for the write.

    http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read(v=vs.110).aspx


    Success
    Cor

    Monday, October 20, 2014 4:13 PM
  •  Hey, thanks for the reply. Here's my code as it stands. 

    Sorry to be so clueless! i only started learning the code last week :) 

          private void save_Click(object sender, RoutedEventArgs e)
            {
                Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog
                {
                    FileName = "AccountLog.txt",
                    Filter = "Text Files (.txt)|*.txt"
                };
                var result3 = saveFileDialog.ShowDialog();
                if (result3.HasValue && result3.Value)
                {
                    using (StreamWriter writer = new StreamWriter(saveFileDialog.OpenFile()))
                    {

                        foreach (var item in lstLog.Items)
                            writer.WriteLine(item.ToString());
                    }
                }
            }


            private void Load_Click(object sender, RoutedEventArgs e)
            {
                lstLog.Items.Clear();
                Microsoft.Win32.OpenFileDialog f = new Microsoft.Win32.OpenFileDialog
                {
                    Filter = "Text Files (.txt)|*.txt"
                };
                var result1 = f.ShowDialog();
                if (result1.HasValue && result1.Value)
                {


                    List<string> lines = new List<string>();
                    using (StreamReader r = new StreamReader(f.OpenFile()))
                    {
                        string line;
                        while ((line = r.ReadLine()) != null)
                        {
                            lstLog.Items.Add(line);

                        }
                    }
                }
            }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Iv'e added this edit to the save button

                       

    using (StreamWriter writer = new StreamWriter(saveFileDialog.OpenFile()))
                    {

        writer.WriteLine("lstLog contains " +lstLog.Items.Count.ToString());
                        foreach (var item in lstLog.Items)
                        writer.WriteLine(item.ToString());
                        writer.WriteLine("accLog1 contains " + accLog1.Items.Count.ToString());
                        foreach (var item in accLog1.Items)
                        writer.WriteLine(item.ToString());
                        writer.WriteLine("accLog2 contains " + accLog2.Items.Count.ToString());
                        foreach (var item in accLog2.Items)
                        writer.WriteLine(item.ToString());
                        writer.WriteLine("profits contains " + profits.Items.Count.ToString());
                        foreach (var item in profits.Items)
                        writer.WriteLine(item.ToString());



                    }

    it now saves like this

    lstLog contains 5
    lstLog line 1

    lstLog line 2

    lstLog line 3

    lstLog line 4

    lstLog line 5

    accLog1 contains 2

    accLog1 line 1

    accLog1 line 2

    etc....

                                        
    how do i now search for this when loading and then load the correct number of lines to each listbox? 


    • Edited by CraigBaines Monday, October 20, 2014 5:03 PM
    Monday, October 20, 2014 4:19 PM
  • ok i changed my save slightly again, i re-read your reply and made more senc of it i think.

    It will save: 

    5 2 1 3

    next 5 lines are loglistboxdata

    next 2 lines are acc1log data

    next 1 line is acclog2 data

    and next 3 lines are profits data.

    Ive added to my load button...

                                          

                    List<string> lines = new List<string>();
                    using (StreamReader r = new StreamReader(f.OpenFile()))
                    {


                        string line;
                        string line1;
                        line1 = r.ReadLine();
                        string[] splitit = line1.Split(' ');
                        string logline = (splitit[0]);
                        string acclog1 = (splitit[1]);
                        string acclog2 = (splitit[2]);
                        string profits = (splitit[3]);

    so im reading the first line, splitting the numbers, an i now have a string containing the correct number of lines for each lsit box.Now how would i tell listbox lostLog to load from line 2 to its string value (5 lines) and so on for the others.. 

    Monday, October 20, 2014 5:22 PM
  • so im reading the first line, splitting the numbers, an i now have a string containing the correct number of lines for each lsit box.Now how would i tell listbox lostLog to load from line 2 to its string value (5 lines) and so on for the others.. 

    (1) Convert the count values from strings to integers.

    (2) Use those integers to control loops which read that many lines from the file and adds
    them to the appropriate listbox.

    You can use TryParse to convert a string to an integer:

    string line;
    string line1;
    line1 = r.ReadLine();
    string[] splitit = line1.Split(' ');
    string logline = (splitit[0]);
    string acclog1 = (splitit[1]);
    string acclog2 = (splitit[2]);
    string profits = (splitit[3]);
    
    int logline_count;
    bool res = Int32.TryParse(logline, out logline_count);
    if (!res)
    {
        // Oops! Not a valid number!
        // ...
    }
    else 
    {
        for (int n = 1; n <= logline_count; ++n)
        { 
        // read next line from file
        // add it to the correct listbox
        // ...                
        }
    
    }
    
    // repeat for each listbox ...
    

    - Wayne

    Monday, October 20, 2014 8:08 PM
  • Im wondering if it is possible for me to save several listbox's into a text file, and then on loads, load them back into the appropriate listbox's.

    Another approach for you to consider and to play with:

    *Instead* of writing a control record at the start of the file with the line counts.
    add a single character to the start of each and every line written to the file which
    identifies the listbox to which this line belongs. Obviously, if a listbox has no lines
    in it when the save is done, there will be no lines in the file with a prefix character
    with that listbox's number.

    When reading the file back for a load operation, use that first character to identify
    which listbox will get this line. Naturally, you remove that prefix ID character when
    adding the line to a listbox.

    - Wayne

    • Proposed as answer by WayneAKing Thursday, October 23, 2014 1:09 PM
    Monday, October 20, 2014 10:20 PM
  • You should try and separate your reading and writing(config file) code into a separate class. There are a few inifile reader available that can really help you.

    http://www.hostprojects.net/cs/35/simple-inifile-reader

    http://stackoverflow.com/questions/217902/reading-writing-an-ini-file

    Monday, October 20, 2014 10:53 PM
  • Thanks guys.

    i think a few days away from it and coming back with a fresh mind made this a whole lot easier haha! 

    i simply made each line start with a different symbol, so text from the lstlog was saved starting with an "@" and data from the other boxes saved starting with a % & and #. 

    Then i just said while the line is not blank, if it contains a @, lstLog.Items.Add(line) else if it contains a % and so on!

    thanks again :) 



    Thursday, October 23, 2014 1:04 PM
  •             foreach (var line in File.ReadLines(FileName))
                {
                    if (line.Substring(0, 1) == "@")
                    {
                        lstLog.Items.Add(line);
                    }
                    else
                    {
                        lst2.Items.Add(line);
                    }
                }
    Try this code

    Happy Coding, RDRaja

    Thursday, October 23, 2014 3:03 PM
  • I know it has been 3 years since this has been talked on but I am having trouble wrapping my brain around how to load 2 listboxes from 1 text file using the prefix identifier.
    Wednesday, February 1, 2017 9:04 PM
  • I am having trouble wrapping my brain around how to load 2 listboxes from 1 text file using the prefix identifier.

    With which part are you having trouble?

    To belabour the obvious, you can't load two listboxes from a single file
    without some criterion/criteria to identify which line from the file goes
    to which listbox. In the case discussed in this thread, the file gets
    saved with an identifying character at the start of each line. That
    character is used when reading the file back to identify to which listbox
    a given line should be sent.

    Before save:

    Lisbox1 has:

    Line 1
    Line 2
    Line 3

    Lisbox2 has:

    Line 99
    Line 88
    Line 77

    These lines are saved to a file, prefixing a single character to each line
    which will identify the listbox to which it belongs. Assuming we use 'X'
    for listbox1 and 'Y' for listbox2 then the lines written to the file will
    be:

    XLine 1
    XLine 2
    XLine 3
    YLine 99
    YLine 88
    YLine 77

    or perhaps in a different order:

    XLine 1
    YLine 99
    XLine 2
    YLine 88
    XLine 3
    YLine 77

    When reading the file and loading two listboxes the prefix identifies which
    listbox gets a given line:

    XLine 1   << Goes to listbox1
    YLine 99    << Goes to listbox2
    XLine 2   << Goes to listbox1
    YLine 88    << Goes to listbox2
    XLine 3   << Goes to listbox1
    YLine 77    << Goes to listbox2

    - Wayne

    Wednesday, February 1, 2017 9:48 PM