Link to home
Start Free TrialLog in
Avatar of jc64
jc64

asked on

ListControl focus setting

I have an MFC form and on it a command button and a list control. When a user presses the button the list control is populated. I want the first line in the list button to be highlighted.

// I use this line to select all the columns of a row.
ListView_SetExtendedListViewStyle(m_lst,LVS_EX_FULLROWSELECT);
Here is the command button code.

void CTtDlg::OnButton1()
{
CListCtrl* pmyListCtrl = &m_lst;

CString strText;
int nColumnCount = m_lst.GetHeaderCtrl()->GetItemCount();

// Insert 10 items in the list view control.
for (int i=0;i < 10;i++)
{
   strText.Format(TEXT("item %d"), i);

   // Insert the item, select every other item.
   m_lst.InsertItem(
      LVIF_TEXT|LVIF_STATE, i, strText,
      (i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,
      0, 0);

   // Initialize the text of the subitems.
   for (int j=1;j < nColumnCount;j++)
   {
      strText.Format(TEXT("sub-item %d %d"), i, j);
      m_lst.SetItemText(i, j, strText);
   }

   m_lst.SetItemData (i, i + 3);

}

//Highlight the first item in the list
 m_lst.SetSelectionMark (0);
 m_lst.SetFocus();

Thanks.

jc

}
ASKER CERTIFIED SOLUTION
Avatar of xu2000
xu2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial