Most Recently Used List in a Combobox

Intro

MFC implements a convenient feature called a most recently used list, or MRU. In SDI and MDI apps generated by AppWizard, the MRU list appears on the File menu. However, another handy place where MRUs are used is in comboboxes. For example, the Start/Run dialog lists the last few programs that you've run in its combobox.

While MFC does not directly support placing an MRU in a combobox, the class CRecentFileList (which implements the File menu MRU list) provides several member functions for maintining, saving, and loading an MRU list. My CMRUComboBox class encapsulates this functionality, and adds the logic necessary to put the MRU in a combobox.

CMRUComboBox was written with MSVC 5.0 and MFC 4.2 Although I have not personally tested it in a Unicode app, it should compile and run with no problems, since the code uses CStrings and LPCTSTRs.

Using an MRU combobox

To use CMRUComboBox, create a combobox control with the CBS_DROPDOWN style. Including CBS_AUTOHSCROLL also is not a bad idea. Use ClassWizard to add a member variable of type CComboBox that is hooked to a combobox control. Then, go to the source and change the type of the variable to CMRUComboBox. CMRUComboBox derives from CComboBox so all the operations you normally do with CComboBoxes will still work the same; CMRUComboBox just adds new methods for managing the MRU list.

In your OnInitDialog() (or other initialization code), you will need to set up a few parameters and flags for the MRU list. At minimum, set the maximum size for the MRU list, and the location in the registry (or INI file) where the list will be saved. There are also a few flags you can optionally set, which are described in the HTML documentation that is in the ZIP file that contains the source code.

Once all that initialization is done, call LoadMRU() to read in the saved contents of the MRU list. When you want to add a string to the list, call AddToMRU(). Duplicate strings are automatically brought to the top of the list, just as in File menu MRU lists. When you want to save the contents of the MRU, call SaveMRU().

Methods List

This is a list of all the new methods available for maintaining an MRU. The full docs are included in the ZIP file that contains the source code files.

BOOL            AddToMRU ( LPCTSTR szNewItem )
void            EmptyMRU()
int             SetMaxMRUSize ( int nMaxSize )
int             GetMaxMRUSize()
void            SetMRURegKey ( LPCTSTR szRegKey )
const CString&  GetMRURegKey()
BOOL            SetMRUValueFormat ( LPCTSTR szValueFormat )
const CString&  GetMRUValueFormat()
BOOL            LoadMRU()
BOOL            SaveMRU()
void            RefreshCtrl()
BOOL            SetAutoRefreshAfterAdd ( BOOL bAutoSave )
BOOL            SetAutoSaveAfterAdd ( BOOL bAutoSave )
BOOL            SetAutoSaveOnDestroy ( BOOL bAutoSave )

Simple example

Here is a sample usage of CMRUComboBox. Suppose you have a Find dialog and you want to list the last few search strings (similar to how MSVC does it). Once you've created a CMRUComboBox member variable in your dialog class as described above, you would do the following in your OnInitDialog() function:

    m_combo.SetMRURegKey ( _T("Find Dlg MRU") );
    m_combo.SetMRUValueFormat ( _T("String%d") );
    m_combo.SetMaxMRUSize ( 12 );
    VERIFY ( m_combo.LoadMRU() );

Then, whenever you want to add a string to the MRU, call

    m_combo.AddToMRU ( szSearchString );

Download demo project - 21KB

Download source and docs - 8KB



Comments

  • Adding large number of records in sorted combobox

    Posted by Legacy on 06/21/2002 12:00am

    Originally posted by: Puneet Jain

    I am finding difficult in initializing the combobox with large number of records and then sorting it.
    Every time, a function is called, its takes a long time to read the records and sort them.
    Is there any way, where after the first use, the process takes a very short time after every reuse.
    I am using VC++ as my application.

    Thanks
    -Puneet

    Reply
  • how to use with ini file ??

    Posted by Legacy on 07/24/2001 12:00am

    Originally posted by: gatman

    can any one tell me how to use with a ini file

    Reply
  • MRUComboBox for free text ( no file name limits ) !

    Posted by Legacy on 07/31/2000 12:00am

    Originally posted by: Yair Konfino

    Hi !
    
    

    The following correction to CMRUComboBox::RefreshCtrl()
    enables the user to enter and display only
    the text he entered :

    void CMRUComboBox::RefreshCtrl()
    {
    CString csBuff;
    int pos

    CString cstrComboText;
    // Save the contents of the edit
    // portion of the combobox.
    GetWindowText (cstrComboText);

    ResetContent();

    for(int i=0;i<m_pMRU->GetSize();i++)
    {
    // Don't add empty strings to the combobox.
    if ((*m_pMRU)[i].GetLength()>0)
    {
    // to work with full file path
    // work only with (*m_pMRU)[i] without csBuff
    csBuff = (*m_pMRU)[i];
    pos = csBuff.ReverseFind('\\');
    csBuff = csBuff.Right(csBuff.GetLength()-pos-1);

    AddString ( csBuff );
    }
    }

    // Restore the editbox text.
    SetWindowText ( cstrComboText );
    }

    Reply
  • Problem in MRU

    Posted by Legacy on 03/30/1999 12:00am

    Originally posted by: Asha

    Thanx for giving this article to code guru.

    I have a problem with MRUCombo , i have used this to display the MRU Search Strings in Find Dialog , and it is working fine in my machine.
    Suppose if i give "microsoft"as search word in my machine it is showing "microsoft"as it is in the combo box , but if i copy my exe and run it on someother's machine it is displaying as "c:\sample\combobox\microsoft" it is showing with complete path not just the word , just help me to solve this problem.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds