Siva
Siva

Reputation: 1411

How to set listcontrol item to be highlighted?

I want to highlight listview item by default.I mean the by default first item should be highlighted .

Actually I did a sample but it is not working :( Here is the code snippet I am using for Inserting Items to the list and setting the first row to get highlighted.

BOOL OnInitDialog()
{
    CDialog::OnInitDialog();

    LVCOLUMN pColumn;
    pColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    pColumn.fmt = LVCFMT_LEFT;
    pColumn.pszText = L"Product Name";
    pColumn.cx = 150;
    pColumn.iSubItem = 1;

    m_ListCtrl.InsertColumn(2, &pColumn);


    ::ZeroMemory(&pColumn, sizeof(LVCOLUMN));
    pColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    pColumn.fmt = LVCFMT_LEFT;
    pColumn.pszText = L"Country";
    pColumn.cx = 150;
    pColumn.iSubItem = 2;

    m_ListCtrl.InsertColumn(3, &pColumn);

    LVITEM lvItem;

    lvItem.mask = LVIF_TEXT;
    lvItem.iItem = 0;
    lvItem.iSubItem = 0;
    lvItem.pszText = L"Himami";
    m_ListCtrl.InsertItem(&lvItem);

    lvItem.mask = LVIF_TEXT;
    lvItem.iItem = 1;
    lvItem.iSubItem = 0;
    lvItem.pszText = L"Shampoo";
    m_ListCtrl.InsertItem(&lvItem);

    //Trying highlight first item in the list.
    m_ListCtrl.SetItemState(0, LVIS_SELECTED ,LVIS_SELECTED);

    m_ListCtrl.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);

    return TRUE;
}

void CListControlFocusDlg::OnListViewItemchanged(NMHDR *pNMHDR, LRESULT      *pResult)
{
     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

    if ((pNMListView->uChanged & LVIF_STATE) && (pNMListView->uNewState & LVNI_SELECTED))
    {
        m_ListCtrl.SetCheck(m_ListCtrl.GetSelectionMark(), TRUE);
        m_ListCtrl.SetSelectionMark(0);
    }
}

After using SetItemState the list control is as follows: List item is not highlighted.

enter image description here

But I want the item to be highlighted as in the below image.

enter image description here

After Implementing tab-order I am able to get the first item in the list highlighted. enter image description here

But I am not able to uncheck the first item until unless I press down arrow.when I press down arrow a rectangular selection is coming on to the first item of the list and now I am able to check or uncheck the highlighted item by pressing space bar.This is how the list looks like after I pressed down arrow.

enter image description here

Can anyone please let me know how can I check or uncheck the first item by pressing space bar without pressing down arrow for the rectangular selection.

Upvotes: 0

Views: 1858

Answers (2)

HariDev
HariDev

Reputation: 540

Use LVN_ITEMCHANGED notification

void OnItemchangedList2(NMHDR* pNMHDR, LRESULT* pResult) 
{
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

    if ((pNMListView->uChanged & LVIF_STATE) && (pNMListView->uNewState & LVNI_SELECTED))
    {
        m_ListCtrl.SetCheck(m_ListCtrl.GetSelectionMark(), TRUE);
    }
}

Upvotes: 0

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

The cancel button looks like it has the focus. The default behavior of Windows is to set the focus to the first item in the tab order. I would suggest you use the resource editor to set the tab order. The easiest way is to set the tab stop order of the items in the dialog and make the list control the first item. The standard keystroke to edit the tab order is to hit Ctrl+D.

Upvotes: 1

Related Questions