Solved

Walking thru a CListCtrl

Posted on 1997-12-23
5
1,025 Views
Last Modified: 2013-11-19
How can I get the values of each row's columns and add them to an array so I can refer to them in my app when needed?? I have a CListCtrl that will have 5 columns per row and ??? rows. I know I can get the number of rows from CLitsCtrl.GetItemCount(), but the GetItem macro has me a bit baffled, I would think that it would take an Index as a parameter but it takes a LV_ITEM structure. Need explained..



0
Comment
Question by:jcorbin
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 100 total points
ID: 1313003
LV_ITEM item has iItem which defines row number and iSubItem wich is zero based index of column. To retreive information of first row and second column for instance declare on stack instance LV_ITEM, set iItem = 0 and iSubItem = 1. Then call CListCtrl::GetItem  
0
 

Author Comment

by:jcorbin
ID: 1313004
I tried this to get the contents of the ListCtrl and write it to a file

        int j;
      char myCol;
      LV_ITEM * myRow;
      CFileException e;
      CString FilePath = "crontab.txt";
      CStdioFile f;

      if(!f.Open(FilePath, CFile::modeCreate|CFile::modeWrite|CFile::typeText|CFile::modeNoTruncate, &e))
      {
            afxDump << "File could not be opened " << e.m_cause << "\n";
      }
      
      for (j=0;j<m_CronList.GetItemCount();j++)
      {
      f.SeekToEnd();
      myRow->iItem = j;
      myRow->iSubItem = 0;
      myCol = m_CronList.GetItem(myRow);

But myCol is returning junk, is my declaration of the LV_ITEM correct?? Sorry I am a VB & Java programmer, just learning to use MFC..

John
0
 
LVL 1

Expert Comment

by:WBerthin
ID: 1313005
I have successfully used the CListCtrl function
CString GetItemText( int nItem, int nSubItem ) const;
to do this, type of thing.

(read nItem as row and nSubItem as Column)

Let me know if you need to see more code.

0
 

Author Comment

by:jcorbin
ID: 1313006
Yes I'd like to see the code, I tried this

CString myRow;
myRow = m_MyListView.GetItemText(1,1);

and it returned nothing even tho there is text in the 1st and 2nd columns....

John

0
 
LVL 1

Expert Comment

by:WBerthin
ID: 1313007
I cannot immediately see the problem with trying
CString myRow = m_MyListView.GetItemText(1,1);

BUT I'll see if I can help here, John.
Is the m_MyListView in your code a object of CListCtrl type?

My code below copies the full contents of a list control item
from a source item to a destination item including all
columns (in report style). The code looks like this
and works just fine for me:

//THE SOURCE ITEM'S INDEX IS nSource
//THE DESTINATION ITEM'S INDEX IS nDestination
//m_ListControl IS A CListCtrl OBJECT
//IN A CLASS DERIVED FROM CView

//GET THE INFORMATION ON THE ITEM TO BE COPIED
char szLabel[256];
LV_ITEM lvi;
ZeroMemory(&lvi, sizeof(LV_ITEM));
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
lvi.stateMask = LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED;
lvi.pszText = szLabel;
lvi.iItem = nSource;
lvi.cchTextMax = 255;
m_ListControl.GetItem(&lvi);

//AT THIS POINT lvi CONTAINS THE INFORMATION ON THE ITEM
//TO BE COPIED

//CREATE THE NEW ITEM
lvi.iItem = nDestination;
m_ListControl.InsertItem(&lvi);

//NOW THE NEW ITEM CONTAINS A COPY OF THE INFORMATION
//EXCEPT FOR THE COLUMN INFORMATION IN REPORT VIEW

//THEN I EXTRACT THE TEXT INFORMATION COLUMN BY
//COLUMN FROM ONE ITEM AND COPY IT TO THE OTHER
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
lvi.mask = LVIF_TEXT;
lvi.iItem = nDestination;
for(int col=1; col<nColumnCount; col++)
{
    strcpy(lvi.pszText,                                                (LPCTSTR)(m_ListControl.GetItemText(nSource, col)));
    lvi.iSubItem = col;
    m_ListControl.SetItem(&lvi);
)

//NOW nDestination IS A COMPLETE COPY OF nSource
//INCLUDING ALL COLUMNS

I assume
    strcpy(lvi.pszText,                                                (LPCTSTR)(m_ListControl.GetItemText(nSource, col)));
could copy those columns into any char* or CString array
with minor modification.

Please let me know how you make out with that.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 78
Prevent this page from creating additional dialogs. 3 395
bigDiff challenge 17 84
canBalance challenge 34 70
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now