Solved

List view control problem

Posted on 1999-01-09
1
200 Views
Last Modified: 2013-11-20
Hi,

I am building a dialog box with 3 edit controls, a list view control in report view and 3 push buttons. With the buttons I want to "Add", "Change" and "Delete" items in the list view control, so for every button I have a button click event handler. See below my event handler for an "Add" button click :

void CBuildShareListDlg::OnAddShareItem()
{
    // Retrieves the following data from the dialog box :
    // CListCtrl    m_lvcShareList;
    // long         m_lVolume;
    // CString      m_strDate;
    // float        m_fRate;
    if ( UpdateData() != TRUE )
        return;
        :
        :
    // Build a CTime object "time" out of m_strDate.

    LV_ITEM      lvItem;
    int          iActItem, iItem;
    TCHAR        szText[3][15];

    lstrcpy( szText[0], ( LPCTSTR ) time.Format( "%d/%m/%Y" ) );
    _stprintf( szText[1], "%f", m_fRate );
    _strprint( szText[2], "%u", ( UINT) m_lVolume );

    iItem = ?  // THAT IS MY PROBLEM.

    for ( int iSubItem = 0; iSubItem < 3; iSubItem++ )
    {
        lvItem.mask = LVIF_TEXT | ( iSubItem == 0 ? LVIF_IMAGE : 0 );
        lvItem.iItem = ( iSubItem == 0 ) ? iItem : iActItem;
        lvItem.iSubItem = iSubItem;
        lvItem.pszText = szText[iSubItem];
        lvItem.iImage = 0;

        if ( iSubItem == 0 )
            ASSERT( ( iActItem = m_lvcShareList.InsertItem( &lvItem ) )!= -1 );
        else
            ASSERT( m_lvcShareList.SetItem( &lvItem ) != -1 );
    }
}

Question 1 :
According to my documentation iItem is a zero based index, but must these indexes be ordered starting from 0 and incremented by 1 each time a new ShareItem is added, or is it just a value that identifies an item in the control.

Question 2 :
When I write my handlers for the "Change" and "Delete" button clicks, how can I figure out which item in the list view is selected. A little bit of code would be very appreciated.


Thanks in advance for everyone that is willing to help me.
0
Comment
Question by:bs161900
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 20 total points
ID: 1327432
#1
The index identifies the item.  But it is important (depending if the list's built in sort is turned off), it gives the position to insert.  If you want to insert at the end of the list, use GetItemCount() to get the number of items currently in the list, and this gives you to index to use when adding items.

#2
int nItem = m_lvc.GetNextItem( nSearchFrom, LVNI_SELECTED ) ;

Gives the _next_ selected item.  If you have a max of 1 item selected (depends on your lists styles, set nSearchFrom to -1)...if you have a list control style allowing multi-seleciton then start from -1.  When you find an item, repeat the call with the return value of the previous call to find the next item, and so.

nItem will be set to -1 when no (more) items are found
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
Creating Labels and Frequency list style reports in SAS code 9 92
Perl Awk Need Help 3 109
Smart Camera scanning and reading information 3 99
Window placement 17 74
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

914 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

22 Experts available now in Live!

Get 1:1 Help Now