5,401,186 members and growing! (15,988 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » ComboBox Controls     Intermediate License: The Code Project Open License (CPOL)

History ComboBox

By Paul S. Vickery

CComboBox with support for item history.
VC6, C++Windows, NT4, Win2K, MFC, VS6, Visual Studio, Dev

Posted: 10 May 2001
Updated: 20 Apr 2007
Views: 165,237
Bookmarked: 54 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
45 votes for this Article.
Popularity: 7.74 Rating: 4.68 out of 5
1 vote, 4.8%
1
0 votes, 0.0%
2
1 vote, 4.8%
3
2 votes, 9.5%
4
17 votes, 81.0%
5

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Paul S. Vickery


Originally from an electronics background, Paul moved into software in 1996, partly as a result of being made redundant, and partly because he was very much enjoying the small amount of coding (in-at-the-deep-end-C) that he had been doing (the only programming he had done prior to that was a little Pascal, and ZX Spectrum Basic!).

Paul swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this he moved to the company for which he currently works, which specialises in Configuration Management, and programs in C/C++, for Windows. He has been gradually moving their legacy C code over to C++/MFC, pulling in other new technologies as he goes where appropriate.

Paul uses Visual Studio 2005, his own in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various other freeware tools.

Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular Combo & List Boxes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 47 (Total in Forum: 47) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThanksmemberranu_hai1:10 25 Feb '08  
GeneralExcellent!memberSteve_Harris5:13 7 Nov '06  
GeneralRe: Excellent!memberPaul S. Vickery5:51 20 Apr '07  
GeneralGood,but...memberFzz17:01 11 Oct '05  
AnswerRe: Good,but...memberPaul S. Vickery5:32 17 Oct '05  
GeneralRe: Good,but...memberFzz3:52 21 Oct '05  
GeneralUNICODE support? test engine won't let you cut/paste greek into comboboxmemberMichael Shiels16:57 8 Feb '05  
Generalmore than one comboBox in the same dialog?memberm3t3ora21:07 11 Aug '03  
GeneralHow to get CHistoryCombo object in toolbar in CMainFrame?memberkydfru22:49 22 Jun '03  
GeneralRe: How to get CHistoryCombo object in toolbar in CMainFrame?memberPaul S. Vickery0:18 23 Jun '03  
GeneralThank you very much for your class and for your help!memberkydfru0:37 23 Jun '03  
GeneralA Bug? It cann't show large text in edit box!memberZhongzhu4:17 14 Apr '03  
GeneralRe: A Bug? It cann't show large text in edit box!memberPaul S. Vickery7:00 14 Apr '03  
GeneralThanks a lot!memberZhongzhu16:32 14 Apr '03  
GeneralRe: A Bug? It cann't show large text in edit box!memberno_reg_name0:25 30 Jun '04  
GeneralRe: A Bug? It cann't show large text in edit box!memberPaul S. Vickery1:58 2 Jul '04  
GeneralRe: A Bug? It cann't show large text in edit box!memberno_reg_name0:39 5 Jul '04  
GeneralSerializing a ComboBoxmemberEPulse22:55 26 Aug '02  
GeneralRe: Serializing a ComboBoxmemberPaul S. Vickery1:01 28 Aug '02  
GeneralRe: Serializing a ComboBoxmemberEPulse1:38 28 Aug '02  
GeneralRe: Serializing a ComboBoxmemberPaul S. Vickery2:35 28 Aug '02  
GeneralRe: Serializing a ComboBoxmemberEPulse0:12 3 Sep '02  
GeneralRe: Serializing a ComboBoxmemberPaul S. Vickery23:38 23 Jun '04  
GeneralWeird - NotificationssitebuilderUwe Keim10:31 26 May '02  
GeneralRe: Weird - NotificationsmemberPaul S. Vickery1:16 6 Jun '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Apr 2007
Editor: Sean Ewington
Copyright 2001 by Paul S. Vickery
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project