catamount

Hi, Expert,

I also found problem in .Net Forms when coding in C#.

I have a ListView (detail mode) and I want to select some related items when an item is cliked.

In the Click event handler, I set some items to selected state.

But when sometimes, these selected items will be unselected automatically.

It's odd.

Is it a bug of Visual Studio or I did the wrong way

Thanks very much!



Re: Smart Devices Native C++ Development Problems in select items in CListCtrl by code

catamount

The problem is sometimes it works and sometime the selected items will be unselected automatically.

I wonder if it is possible to set multiple items selected when one item is clicked by user.

It's a simple and useful function. CListCtrl should provide it.

Thanks





Re: Smart Devices Native C++ Development Problems in select items in CListCtrl by code

catamount

I try to select multiple items in CListCtrl by code.

But sometimes, the follwong code failed.

Please tell me how to select multiple items when user click one item in CListCtrl.

Thanks.

ON_NOTIFY(NM_CLICK, IDC_LIST_COMMAND, OnClickListCtrl)

void CDlgHistory:nClickListCtrl(NMHDR *pNMHDR, LRESULT *pResult)

{

LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

int nItem = -1;

nItem = m_cListCtrl.GetNextItem(nItem, LVNI_SELECTED);

SelectItems(nItem);

*pResult = 0;

}

void CDlgHistory:electItems(int nItem)

{

int iTotal = m_pHistory->GetTotal();

for (int i = 0; i < nItem; i++)

{

m_cListCtrl.SetItemState(i, LVIS_FOCUSED, LVIS_FOCUSED);

m_cListCtrl.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);

}

}

ON_NOTIFY(NM_CLICK, IDC_LIST_COMMAND, OnClickListCtrl)

void CDlgHistory:nClickListCtrl(NMHDR *pNMHDR, LRESULT *pResult)

{

LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

int nItem = -1;

nItem = m_cListCtrl.GetNextItem(nItem, LVNI_SELECTED);

SelectItems(nItem);

*pResult = 0;

}

void CDlgHistory:electItems(int nItem)

{

int iTotal = m_pHistory->GetTotal();

for (int i = 0; i < nItem; i++)

{

m_cListCtrl.SetItemState(i, LVIS_FOCUSED, LVIS_FOCUSED);

m_cListCtrl.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);

}

}





Re: Smart Devices Native C++ Development Problems in select items in CListCtrl by code

Zero Dai - MSFT

Please don't cross posting. Thanks!




Re: Smart Devices Native C++ Development Problems in select items in CListCtrl by code

Zero Dai - MSFT

Hi catamount,

Recently, I have done some search in all the smart device forum, and find some topic on how to implement multi-selection in ListView (need some P/Invoke technology). Hope that may help you.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1463179&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=143697&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=403396&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=149812&SiteID=1

Regards,

Zero Dai - MSFT






Re: Smart Devices Native C++ Development Problems in select items in CListCtrl by code

Zero Dai - MSFT

First thanks to Tim Wilson (.Net Compact Framework MVP)...

Hi catamount,

You can use the following code to make the ListView multiple selectable.

Code Snippet

[DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

//...

public Form1()
{
InitializeComponent();

IntPtr hWnd = this.listView1.Handle;
int style = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, (style & ~LVS_SINGLESEL));
}

This is for managed code, if you want to achieve with native C++, call GetWindowLong and SetWindowLong function with the parameter correspondent directly.

Thanks,

Zero Dai - MSFT