Click here to Skip to main content
Licence CPOL
First Posted 10 May 2001
Views 233,380
Downloads 3,060
Bookmarked 82 times

History ComboBox

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

About the Author

Paul Vickery

Software Developer (Senior)

United Kingdom United Kingdom

Member

Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!
 
I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.
 
In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).
 
For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.
 
When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood job! PinmemberSergey Podobry0:38 12 Nov '10  
GeneralQuestion PinmemberPiccinano1:01 14 Mar '10  
GeneralRe: Question PinmemberDieter Hammer22:46 9 Jun '10  
GeneralI need help Pinmembersebilkullan22:18 16 Dec '09  
GeneralThanks Pinmemberranu_hai0:10 25 Feb '08  
GeneralExcellent! PinmemberSteve_Harris4:13 7 Nov '06  
GeneralRe: Excellent! PinmemberPaul S. Vickery4:51 20 Apr '07  
GeneralGood,but... PinmemberFzz16:01 11 Oct '05  
AnswerRe: Good,but... PinmemberPaul S. Vickery4:32 17 Oct '05  
GeneralRe: Good,but... PinmemberFzz2:52 21 Oct '05  
GeneralUNICODE support? test engine won't let you cut/paste greek into combobox PinmemberMichael Shiels15:57 8 Feb '05  
Questionmore than one comboBox in the same dialog? Pinmemberm3t3ora20:07 11 Aug '03  
QuestionHow to get CHistoryCombo object in toolbar in CMainFrame? Pinmemberkydfru21:49 22 Jun '03  
AnswerRe: How to get CHistoryCombo object in toolbar in CMainFrame? PinmemberPaul S. Vickery23:18 22 Jun '03  
GeneralThank you very much for your class and for your help! Pinmemberkydfru23:37 22 Jun '03  
GeneralA Bug? It cann't show large text in edit box! PinmemberZhongzhu3:17 14 Apr '03  
GeneralRe: A Bug? It cann't show large text in edit box! PinmemberPaul S. Vickery6:00 14 Apr '03  
GeneralThanks a lot! PinmemberZhongzhu15:32 14 Apr '03  
GeneralRe: A Bug? It cann't show large text in edit box! Pinmemberno_reg_name23:25 29 Jun '04  
GeneralRe: A Bug? It cann't show large text in edit box! PinmemberPaul S. Vickery0:58 2 Jul '04  
GeneralRe: A Bug? It cann't show large text in edit box! Pinmemberno_reg_name23:39 4 Jul '04  
GeneralSerializing a ComboBox PinmemberEPulse21:55 26 Aug '02  
GeneralRe: Serializing a ComboBox PinmemberPaul S. Vickery0:01 28 Aug '02  
GeneralRe: Serializing a ComboBox PinmemberEPulse0:38 28 Aug '02  
GeneralRe: Serializing a ComboBox PinmemberPaul S. Vickery1:35 28 Aug '02  
The main problem isn't with the serialization. It is this:
FilterCriteria = (CSerialComboBox*) GetDlgItem(IDC_CRITERIA);
You can't simply cast a CWnd* to a CSerialComboBox*. Whatever you cast it to it's still really a CWnd*. If you break in a debugger after the line above, and look at what it says FilterCriteria is, it will tell you it's a CTempWnd*.
 
You need to subclass the control to a CSerialComboBox to be able to use it as that. This is normally done by setting up a member variable mapping using the ClassWizard. This subclasses the window handle.
 
When you call the MFC GetDlgItem(), the function first gets the HWND for the control, and then it looks it up to find the CWnd* it is currently mapped to (see documentation for CWnd::FromHandle). In your case it's not mapped to anything so you get a CTempWnd object. The function then always returns it as a CWnd*.
 
If you had already subclassed it to a CSerialComboBox, then the pointer returned would be a CSerialComboBox* (albeit upcast to a CWnd*), and the debugger would show it as that. (Using something like DYNAMIC_DOWNCAST can help with making sure you have the correct cast.)
 
So, to fix your problem, map the control to a CSerialComboBox.
 
As for the functions not being found, I assume you mean in the ClassView. The best remedy for this is to delete your workspace's ncb file. You will need to close the workspace first.
 
Perhaps I could make a suggestion about your Serialize() function. When serializing a list of items where the number of items is dynamic, it s a good idea to store the number of items first, then store each item in a loop. Then, on loading, you will then first read the number of items, then you know how many times to loop and read the actual items.
 
I hope this helps!

 
---
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120604.1 | Last Updated 20 Apr 2007
Article Copyright 2001 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid