Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

History ComboBox

0.00/5 (No votes)
20 Apr 2007 1  
CComboBox with support for item history.

Sample Image - HistoryCombo_demo.jpg

Introduction

The History Combo class CHistoryCombo is based on CComboBox, and includes support for keeping item history, including reading from and saving to the registry. It can also be used to display the contents of an existing CRecentFileList object.

How to use it

Using the CHistoryCombo class is very straightforward. Follow the steps below to add one to an existing project:

  1. After putting the source files (HistoryCombo.cpp and HistoryCombo.h) into the directory you wish to use them from, add the files to your Visual Studio project.
  2. In the resource editor, add a combo where you wish.
  3. In Class Wizard, add a member variable for your combo control, selecting "Control" from the "Category" list, and selecting "CHistoryCombo" from the "Variable Type" list. (If CHistoryCombo does not appear in the list, you may need to delete your class wizard file (.clw) and regenerate it). I will assume your control variable name is m_comboHistory.
  4. Add a handler for WM_INITDIALOG in your dialog class if you don't already have one, and add the following code to it:
    m_comboHistory.LoadHistory("Settings", "HistoryCombo");

    (Note: you can specify any string you wish for the section, and key-prefix.)

  5. In your handler for the IDOK button, or wherever you choose, add the following:
    m_comboHistory.SaveHistory();

That's all you need to do.

Serialization, and using a string for storage

For examples on how to use the CArchive support, and the loading and saving from and to a string, see the demo program.

Documentation

Each function is documented in the CPP file, but here is a list of functions in CHistoryCombo:

CHistoryCombo(BOOL bAllowSortStyle = FALSE);
CString LoadHistory(LPCTSTR lpszSection, LPCTSTR lpszKeyPrefix, 
    BOOL bSaveRestoreLastCurrent = TRUE, LPCTSTR lpszKeyCurItem = NULL);
CString LoadHistory(CRecentFileList* pListMRU, BOOL bSelectMostRecent = TRUE);
void SaveHistory(BOOL bAddCurrentItemtoHistory = TRUE);
virtual void Serialize(CArchive& ar);
void SaveHistory(CArchive& ar, BOOL bAddCurrentItemtoHistory = TRUE);
void LoadHistory(CArchive& ar);
CArchive& operator<<(CArchive& ar, CHistoryCombo& ob);
CArchive& operator>>(CArchive& ar, CHistoryCombo& ob);
CString SaveHistoryToText(CString& sHistory, BOOL bAddCurrentItemToHistory = 
    TRUE, LPCTSTR lpszDelims = _T("\r\n"));
void LoadHistoryFromText(LPCTSTR lpszHistory, LPCTSTR lpszLastSelected = NULL,
    LPCTSTR lpszDelims = _T("\r\n"));
int AddString(LPCTSTR lpszString);
void SetMaxHistoryItems(int nMaxItems);
void ClearHistory(BOOL bDeleteRegistryEntries = TRUE);
void StoreValue(BOOL bIgnoreIfEmpty = TRUE);
void SetAutoComplete(BOOL bAutoComplete = TRUE);
BOOL GetAutoComplete();
static void SetAutoCompleteDefault(BOOL bAutoComplete = TRUE);
static BOOL GetAutoCompleteDefault()

History

Version 3.1 - 20 Apr 2007

Version 3.0 - 22 Jun 2004

  • Added serialization to/from a CArchive object (suggested by EPulse), which can be used by one of the three methods:
    • calling Serialize() directly with a CArchive object
    • calling the LoadHistory()/SaveHistory() overloads which take a CArchive object reference
    • using the insertion operator overloads >> and <<

      (The only difference between these methods is that using SaveHistory() gives you the option of not adding the current item to the history.)

  • Added loading/saving from/to a CString object (suggested by Uwe Keim)

Version 2.1 - 09 Jul 2003

  • Updated to support Unicode.

Version 2 - 01 May 2002

  • Removed CBS_SORT on creation, if specified.
  • Added option to allow the sort style to be set, if required.
  • Fixed SetMaxHistoryItems, so it removes old entries from the list to ensure that there are no more than the maximum. Also, made SaveHistory remove redundant profile entries above the maximum.
  • Uses WriteProfileString to remove profile entries rather than CRegKey.

Version 1 - 12 Apr 2001

  • First version.

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