Click here to Skip to main content
12,292,953 members (64,438 online)
Click here to Skip to main content
Add your own
alternative version


56 bookmarked

Simple Row Insert for CListCtrl

, 9 Jul 2000
Rate this:
Please Sign up or sign in to vote.
This simple CListCtrl extension allows easy row insertion when in Report mode.
<!-- STEP 2. Include download and sample image information -->

Ever been annoyed at the amount of code you need just to insert a few rows and their columns' text into a CListCtrl while using the 'Report' style?

This little MFC subclassing tutorial provides a handy little enhancement to the standard CListCtrl, which can make life easier for both beginners and not_so_beginners alike.

The code and text that follows assumes you have generated a typcal Dialog based application with the MFC Application (.exe) Appwizard in MS Visual C++.

Derive a new CListCtrl based class (here CMyListCtrl).  In the VC IDE, this is as simple as selcting 'Add Class...' from the MFC Class wizard then, in the new class dialog, naming the control and selecting CListCtrl as the base class in the drop down list.

To use the new class, start by adding a CListCtrl to the dialog (be sure to select 'Report' in the Styles page of the proprties for the new control) then, in the Member Variables page of the MFC Class Wizard (ctrl+w), add a control variable for the newly created item (IDC_LIST1, or whatever ID you supplied) - but instead of accepting the default CListCtrl as the type, you will be able to select the new CMyListCtrl type. Call the variable m_myList.


<font color="#0000FF">#include</font> "MyListCtrl.h"

in the header of the dialog class (in this case, the main dialog of the app) and you're set.

In the code that VC generates for the new class, add the following: 

<font color="#008040">// put this into the header file, in the public: section
<font color="#008040">// and this into the cpp file, in the public: section</font>
<font color="#008040">// In your program the list control can now be filled like this:

To test, you might add a command button that executes the above code.  Also, this assumes that you have initialized the list with 3 columns - for the sake of this simple example, you might provide the following in the OnInitDialog method of the dialog class:

<font color="#008040">// TODO: Add extra initialization here</font>
m_myList.InsertColumn(0, "Column 1");
m_myList.InsertColumn(1, "Column 2");
m_myList.InsertColumn(2, "Column 3");

m_myList.SetColumnWidth(0, 100);
m_myList.SetColumnWidth(1, 100);
m_myList.SetColumnWidth(2, 100);




This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Daniel Zuppinger
Chief Technology Officer
Switzerland Switzerland
Professional IT developer since 1983. First projects with Cobol, then Pascal, Modula2, C and since Visual C++ 1.0 also with C++. Works since 1986 as Consultant, between 1990 and 2008 for Infobrain in Switzerland, currently employed by enValue (also Switzerland).
Current field is the project management and development of software for Bank applications.
Married, two daughters, Hobbies : Paragliding, Orienteering, Mountainbiking, Photography

You may also be interested in...

Comments and Discussions

GeneralListCtrl Pin
rel11-Jul-01 23:48
memberrel11-Jul-01 23:48 
GeneralRe: ListCtrl Pin
David Fleming7-Mar-02 4:22
memberDavid Fleming7-Mar-02 4:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160518.1 | Last Updated 10 Jul 2000
Article Copyright 2000 by Daniel Zuppinger
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid