Click here to Skip to main content
15,849,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void CDlgCorrection::RowSelected()
{
	m_bInizio = false;
	for (int i = 0; i < m_astrPath.GetSize(); i++)
	{
		m_lista.SetItemState(i, 0, LVIS_SELECTED);
	}

	m_lista.SetItemState(m_nRow, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
	m_nRowSelected = m_nRow;
	m_lista.SetFocus();
	m_lista.SetSelectionMark(m_nRow);
}
I used this code to select a row in my CListCtrl but if I click in my CListCtrl the row is selected so then I have 2 rows selected..how Can I deselect a row before I click in my list

What I have tried:

I tried to write:
C++
m_lista.SetItemState(m_nRow, ~LVIS_SELECTED | ~LVIS_FOCUSED, ~LVIS_SELECTED | ~LVIS_FOCUSED);


I tried also
C++
if(m_nRow!= -1)
	m_lista.SetItemState(m_nRow, ~LVIS_SELECTED, LVIS_SELECTED);

	POSITION			pos = NULL;
	INT_PTR				nPos;

	pos = m_lista.GetFirstSelectedItemPosition();
	if (pos)
	{
		nPos = m_lista.GetNextSelectedItem(pos);
	}
	else
	{
		nPos = -1;
	}
but I have anyway 2 rows selected

....I solved:
C++
void CDlgCorrection::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{		
		m_lista.SetItemState(m_nrow, 0, LVIS_SELECTED);
		
		m_nRowSelected = -1;
		m_lista.SetFocus();
		m_lista.SetSelectionMark(-1);
}
Posted
Updated 21-Dec-23 4:19am
v8

If having two rows selected is a problem then set the control's properties to allow single selections only. That can be done with the resource editor or in code.

You might want to consider using one of the many derived list control classes available at this site. I use this one: CReportCtrl - An extremely convenient version of report-style CListCtrl[^].
 
Share this answer
 
Comments
Member 14594285 23-Jun-23 11:18am    
yes, I allowed single selection only in my resource editor
Try:
C++
m_lista.SetItemState(m_nRow, 0, LVIS_SELECTED | LVIS_FOCUSED);

Which should set the two state bits to zero.
 
Share this answer
 
Comments
Member 14594285 23-Jun-23 8:22am    
If I use this row of code it doesn't solve
Richard MacCutchan 23-Jun-23 8:32am    
Sorry, but I do not have MFC on my system so I am not able to test this. However, saying "it doesn't solve", does not give ius any idea what may be the problem. So maybe you could use the Improve question link above, and add complete details of your modified code, and what is not working.
Try this. In your OnCreate functions

C++
GetListCtrl().SetExtendedStyle(LVS_EX_FULLROWSELECT);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900