Click here to Skip to main content
15,890,882 members
Articles / Desktop Programming / MFC

A Class to Save and Load Listbox Data

Rate me:
Please Sign up or sign in to vote.
3.91/5 (13 votes)
2 Jan 20021 min read 173.7K   2.2K   44   19
CListBoxSafe is a class that can save data from a listbox to file and load data from a file into a listbox.

Introduction

This is my first attempt at writing a class and at writing a Code Project article so I hope it will be of some use. I noticed I was using a lot of listbox saving/restoring in my applications so I decided to write a class for this. The class does not contain error handling but it should work fine. To use it, just add the files to your project and include ListBoxSafe.h. There are (only) two functions you can use (simplicity):

C++
void LoadList(CListBox &list, CString FileName, int MaxItems, int ItemLen);

and:

C++
void SaveList(CListBox &ListToFill, CString FileName, int MaxItems, int MaxItemLen);

list/ListToFill is the ListBox that you need to save or fill.
FileName is the name of the file from which you want to load or to which you want to save to.
MaxItems is the maximum amount of items to be saved or loaded.
ItemLen/MaxItemLen is the maximum length of a listbox item (should be the same if you save and load).

You can give MaxItems the value 0, and it will be 999 by default.
You can give MaxItemLen/ItemLen the value 0, and it will be 30 by default.

Example

C++
CListBoxSafe::SaveList(m_lMyList,"test.dat",0,0);
CListBoxSafe::LoadList(m_lDataList,"data.txt",0,0);

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Belgium Belgium
bla bla bla

Comments and Discussions

 
Generalhelp me Pin
Jazz21-Dec-07 22:01
Jazz21-Dec-07 22:01 
Generalsave and load listbox data Pin
Anuj Vohra9-May-06 21:13
Anuj Vohra9-May-06 21:13 
GeneralNice idea but severely flawed! Pin
Steve Riggall3-Mar-05 4:48
Steve Riggall3-Mar-05 4:48 
This is a neat idea however it is very severly flawed with various bugs:

In the Save() routine you are allocating a whole stack of CString objects which are never being freed up which means everytime this routine is called it will leak memory (the same for loading too, but worse as more memory is leaked as you are also allocating bytes up to the 'max length' that are never freed)

In these routines you do not need to allocate any memory in this way, just get rid and use a simple CString object on the heap. In the Load() routine you may want to allocate a number of bytes but you MUST remember to do a delete[] temp; Also in this routine MaxItems is a redundant variable. It does nothing. It would be better to have the default as the number of items in the list instead of the arbitrary value of 999.

The actual file format being used is very limited and EXTREMELY inefficient. A better idea might be to encode a control character that acts as an end of line marker - normally for text files this would be a simple carriage return and there are standard runtime library methods that can do this for free!

Below is the kind of thing that I meant - just the SaveList() routine but should get you on the right lines. I have removed the MaxItems and MaxItemLen stuff as they are pretty much pointless, and this routine uses a simple CR/LF pair to determine the end of each line. Obviously the LoadList() routine would need to take account of this. There are in fact RTL methods that can read and write text files line by line (I thing these are fgets and fputs but not sure). Also, I have not tested this code in a compiler for syntax as its just to show you what I mean..

<br />
void CListBoxSafe::SaveList(CListBox &list, CString FileName)<br />
{<br />
     int nCount = list.GetCount();<br />
     CString strData;<br />
     CFile sav;<br />
<br />
     if(sav.Open(FileName, CFile::modeCreate|CFile::modeWrite))<br />
     {		<br />
          for(int i=0;i<=count-1;i++)<br />
          {  <br />
               list.GetText(i,strData);<br />
               sav.Write(strData,strData.GetLength());<br />
               sav.Write("\r\n",2);       <br />
          }<br />
     }	<br />
}<br />


Keep up the good work and hope this helps!
GeneralRe: Nice idea but severely flawed! Pin
Kuniva3-Mar-05 12:00
Kuniva3-Mar-05 12:00 
GeneralRe: Nice idea but severely flawed! Pin
wise.bear14-Apr-10 17:47
wise.bear14-Apr-10 17:47 
GeneralWorking good, but... Pin
macrophobia15-Feb-05 15:36
macrophobia15-Feb-05 15:36 
Generali cant downloadit Pin
titanflan4-Jan-03 11:39
titanflan4-Jan-03 11:39 
Generalerror Pin
Anonymous2-Sep-02 9:52
Anonymous2-Sep-02 9:52 
QuestionHow can i use this class? Pin
Anonymous15-Aug-02 22:27
Anonymous15-Aug-02 22:27 
GeneralExcellent! Pin
Selevercin12-Jan-02 6:04
Selevercin12-Jan-02 6:04 
GeneralIts good Pin
Nnamdi Onyeyiri3-Jan-02 10:06
Nnamdi Onyeyiri3-Jan-02 10:06 
GeneralRe: Its good Pin
Kuniva4-Jan-02 2:07
Kuniva4-Jan-02 2:07 
GeneralRe: Its good Pin
MNS14-Jan-03 0:18
MNS14-Jan-03 0:18 
GeneralRe: Its good Pin
Selevercin12-Jan-02 6:07
Selevercin12-Jan-02 6:07 
GeneralRe: Its good Pin
Bret Faller2-Aug-02 13:21
Bret Faller2-Aug-02 13:21 
GeneralRe: Its good Pin
Nnamdi Onyeyiri2-Aug-02 23:04
Nnamdi Onyeyiri2-Aug-02 23:04 
GeneralRe: Its good Pin
chen10-Sep-02 15:51
chen10-Sep-02 15:51 
GeneralRe: Its good Pin
jaysnanavati24-Jan-09 2:04
jaysnanavati24-Jan-09 2:04 
AnswerRe: Its good Pin
Bret Faller24-Jan-09 9:24
Bret Faller24-Jan-09 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.