Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Class to Save and Load Listbox Data

, 2 Jan 2002
CListBoxSafe is a class that can save data from a listbox to file and load data from a file into a listbox.
listboxsafe_src.zip
// ListBoxSafe.cpp: implementation of the CListBoxSafe class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ListBoxSafe.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CListBoxSafe::CListBoxSafe()
{

}

CListBoxSafe::~CListBoxSafe()
{

}

void CListBoxSafe::SaveList(CListBox &list, CString FileName, int MaxItems, int MaxItemLen)
{
int count = list.GetCount();
CString *pData = new CString[count];
CFile sav;

// Default values
if(MaxItems==0)
{
	MaxItems = 999;
}
if(MaxItemLen==0)
{
	MaxItemLen = 30;
}


if (sav.Open(FileName, CFile::modeCreate + CFile::modeWrite))
{		

for(int i=0;i<=count-1;i++)
{
list.GetText(i,pData[i]);
sav.Seek((long)(i*MaxItemLen),CFile::begin);
sav.Write(pData[i],pData[i].GetLength());
}
	
}	

}

void CListBoxSafe::LoadList(CListBox &ListToFill, CString FileName, int MaxItems, int ItemLen)
{
// Default values
if(MaxItems==0)
{
	MaxItems = 999;
}
if(ItemLen==0)
{
	ItemLen = 30;
}

	
CString *pData = new CString[MaxItems];
CFile loa;
TCHAR *temp = new TCHAR[ItemLen];
int Bytes = 0;

if (loa.Open(FileName, CFile::modeRead))
{		
for(int i=0;i<=MaxItems;i++)
{
	loa.Seek((long)(i*ItemLen),CFile::begin);
	Bytes = loa.Read(temp,ItemLen);
	temp[Bytes] = NULL;
	pData[i] = temp;
	pData[i].TrimRight();
	if(pData[i]!="")
	{
	ListToFill.AddString(LPCTSTR(pData[i]));
	}
	else
	{
		return;
	}
}

}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

Kuniva

Belgium Belgium
bla bla bla

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Jan 2002
Article Copyright 2002 by Kuniva
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid