?
Solved

Walking thru a CListCtrl

Posted on 1997-12-23
5
Medium Priority
?
1,036 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 400 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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