CComboBox with support for history

Environment: VC6

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 strings you wish for the section, and key-prefix)

5. In your handler for the IDOK button, or whereever you choose, add the following:

m_comboHistory.SaveHistory();

That's all you need to do.

Documentation

Each function is documented in the cpp file. 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);
int AddString(LPCTSTR lpszString);
void SetMaxHistoryItems(int nMaxItems);
void ClearHistory(BOOL bDeleteRegistryEntries = TRUE);
void StoreValue(BOOL bIgnoreIfEmpty = TRUE);

Downloads

Download source - 4.52 Kb

Download demo - 118 Kb

May 8, 2002 - Version 2

  • 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.
  • use WriteProfileString to remove profile entries rather than CRegKey.

April 12, 2001 - First Version



Comments

  • How we can add an string to list box item of combobox by code?

    Posted by Legacy on 05/01/2003 12:00am

    Originally posted by: zeinalzadeh

    How we can add an string to list box item of combobox by code?

    Reply
  • Question about Copy Paste in ComboBox

    Posted by Legacy on 12/16/2002 12:00am

    Originally posted by: Mahesh Shimpi

    Hi,

    I am using this combo box in the Rebar of my SDI application. It behaves like the web-browser Address bar, remembering the IP addresses and URLs.
    However I am not able to copy paste into this combo box from outside.
    I tried a sample Dialog based application and the copy-paste works fine in that.

    Is the copy paste message getting lost in my SDI application as the Combo box in the Rebar? Do I need to explicitly handle the message?

    Please assist.

    Thanks
    Mahesh

    Reply
  • Turning of the sort style of the combo

    Posted by Legacy on 03/13/2002 12:00am

    Originally posted by: J.E.E. No�l

    This is your code for PreCreateWindow(..)
    
    

    BOOL CHistoryCombo::PreCreateWindow(CREATESTRUCT& cs)
    {
    // warn if creating with CBS_SORT style
    // (unfortunately we can't turn it off)
    if (cs.style & CBS_SORT)
    TRACE("WARNING: Creating History combo with CBS_SORT style\n");
    return CComboBox::PreCreateWindow(cs);
    }


    Why not use the following code for PreCreateWindow(..)?
    This does remove the CBS_SORT style.

    BOOL CHistoryCombo::PreCreateWindow(CREATESTRUCT& cs)
    {
    // remove the CBS_SORT style
    cs.style&=~CBS_SORT;

    return CComboBox::PreCreateWindow(cs);
    }

    Reply
  • Why not use CDialog::DoDataExchange(CDataExchange* pDX)

    Posted by Legacy on 08/08/2001 12:00am

    Originally posted by: Patrick Koorevaar

    Thank you very much for this code.
    

    I think the data exchange can be done simpler with the help of the DoDataExchange function. I did the data exchange by adding the following lines to the DoDataExchange function:

    if(pDX->m_bSaveAndValidate)
    m_comboHistory.SaveHistory();
    else
    m_comboHistory.LoadHistory("Settings", "HistoryCombo");

    Regards,

    Patrick

    Reply
  • Problem with code.

    Posted by Legacy on 05/22/2001 12:00am

    Originally posted by: Rob Jones

    Hello,
    
    I have a combo box that has about 40 items pre-configured in the combo box/drop-down list.. I have implemented your code in hopes to save any extra data that is inputted into the combo box that is not already there.. The problem is if I pick something that is already listed in the dropdown it saves that information to the registry. Then the next time I use the application it displays multiple occurrences of the same item..

    Not sure if this is really a problem with your code, maybe I should figure out a way to look at the string that is being inputted into the combo box and compare it to the hard coded string and if it already exists don’t save it..

    Let me know if you have any ideas..

    Thanks,
    Rob

    Reply
  • destructor

    Posted by Legacy on 05/22/2001 12:00am

    Originally posted by: sally

    why not let the destructor save the history?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds