SetItemState no effect to

Posted on 2012-08-21
Last Modified: 2012-08-28

I use a  SetItemState(0,LVIS_SELECTED, LVIS_SELECTED); to select a row in a CListCtrl.
But it seems that this do not set the state well. because while checking in:

    if ((pNMLV->uChanged & LVIF_STATE) 
        && (pNMLV->uNewState & LVNI_SELECTED))

Open in new window

the uNewState is not "LVNI_SELECTED", it is "LVNI_SELECTED" after clicking in the list. What is the flaw?

Question by:Ocrana
    1 Comment
    LVL 30

    Accepted Solution

    Hi Ocrana,

    hard to say what the problem is, could you maybe post some more relevant code here?

    The only 'bug' I see is that you use LVNI_SELECTED instead of LVIS_SELECTED. The first one is intended to be used with GetNextItem, not as item state. But since both are defined to be 2 it should work anyway.

    I made a little test dialog application and there it worked fine. I just created a dialog resource with a list control IDC_LIST1, added some code in OnInitDialog to fill the list and to select a list item and added a LVN_ITEMCHANGED notification handler. Here the relevant code parts:
    // in header
    class CTestDlg : public CDialog
    	afx_msg void OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult);
    // in CPP file
    BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
    BOOL CTestDlg::OnInitDialog()
    	CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem( IDC_LIST1 );
    	CString strItem;
    	int nIndex = 0;
    	pListCtrl->InsertColumn( 0, _T( "Col 1" ), LVCFMT_LEFT, 100 );
    	pListCtrl->InsertColumn( 1, _T( "Col 2" ), LVCFMT_LEFT, 100 );
    	for ( int n = 0; n < 10; n++ )
    		strItem.Format( _T( "Row %i" ), n + 1 );
    		pListCtrl->InsertItem( n, strItem );
    		strItem.Format( _T( "Subitem %i" ), n + 1 );
    		pListCtrl->SetItemText( n, 1, strItem );
    	pListCtrl->SetItemState( 3, LVIS_SELECTED, LVIS_SELECTED );
    	return TRUE;  // return TRUE  unless you set the focus to a control
    void CTestDlg::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
    	if ( (pNMLV->uChanged & LVIF_STATE) && (pNMLV->uNewState & LVNI_SELECTED) )
    		AfxMessageBox( _T( "Yep!" ) );

    Open in new window

    When I run this application the message box showing "Yep!" pops up immediateley (as expected) and when it's closed the 4th item is selected in the list control.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now