A VisualStudio-like ListBox class

Sample Image

Environment: VC6 SP1, NT4 SP5, WIN 95/98

This article presents an extended listbox control. Its look and feel resembles the one that you can find in the Tools->Options dialog of Visual Studio environment.
As you can see from the picture, this new listbox allows in-place item editing, and adds also the possibility to show a "browse" button near the edit field.
Moreover, since it is derived from a drag listbox, it is also possible to reorder items through drag and drop, or through Alt+Up/Alt+Down keys.

I also wrote a simple "buddy" class, that you can see in the upper part of the picture, that makes the control even easier to use.

How To Use It

To use this listbox class, simply drop a normal listbox on a dialog and associate it with a CListBoxEx class:

// This class overrides the OnBrowseButton method.
class CMyListBoxEx: public CListBoxEx
{
public:

 virtual void OnBrowseButton( int iItem )
 {
  iItem;
  CSHBrowseDlg dlgBrowse;
  if ( dlgBrowse.DoModal() ) SetEditText( dlgBrowse.GetFullPath() );
 };
};

class CListBoxExDemoDlg : public CDialog
{
// Construction
public:
 // standard constructor
 CListBoxExDemoDlg(CWnd* pParent = NULL);	

// Dialog Data
 //{{AFX_DATA(CListBoxExDemoDlg)
 enum { IDD = IDD_LISTBOXEXDEMO_DIALOG };
 CMyListBoxEx	m_ListBoxEx;
 //}}AFX_DATA
};
It's as easy as that! The OnBrowseButton() is called synchronously when the user clicks on the "..." button. Other events that are called are OnBeginEditing() and OnEndEditing().

If you want to use also the buddy class, drop a button on the dialog, change its properties and make it owner-draw and with a client edge.
Then, in your dialog OnInitDialog() subclass the button control in this way:
// Add the listbox buddy
m_ListBoxExBuddy.SubclassDlgItem( IDC_LISTBUDDY, this );
m_ListBoxExBuddy.SetListbox( &m_ListBoxEx );


Other improvements

I added a lot of (I think) useful methods, that add quite a bit of functionality to the raw listbox and make it a bit more user-friendly:

// Editing & drag are anabled by default
void AllowEditing( BOOL bAllowEditing = TRUE )
{
m_bAllowEditing = bAllowEditing;
};

void AllowDrag( BOOL bAllowDrag = TRUE )
{
m_bAllowDrag = bAllowDrag;
};

// Editing methods
void BeginEditing( int iItem );
void EndEditing( BOOL fCancel );

// Add a new empty string and begin editing
void EditNew();

void SetEditStyle( DWORD dwEditStyle );

HWND GetEditHandle() const;

void SetEditText( const CString & strNewText ) const;

// Item methods
int MoveItemUp( int iItem );
int MoveItemDown( int iItem );
void SwapItems( int iFirstItem, int iSecondItem );

void SetItem( int iItem, LPCTSTR szItemText, DWORD dwItemData );
void SetItemText( int iItem, LPCTSTR szItemText );

// Virtual (overridables) events
virtual BOOL OnBeginEditing( int iItem );
virtual BOOL OnEndEditing( int iItem, BOOL fCanceled );

virtual void OnBrowseButton( int iItem );

Downloads

Download demo project - 50 Kb
Download source - 17 Kb


Comments

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

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds