Solved

Walking thru a CListCtrl

Posted on 1997-12-23
5
1,034 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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