List Control changing sub items image.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: List Control changing sub items image.

  1. #1
    Join Date
    Oct 2005
    Posts
    27

    How to tell what sub item has been clicked on?

    Hi All,

    I've created a List Control that displays in the following attached image. The checkboxes come from an ImageList, also attached and shown below.

    What I'd like to do now is provide the functionality to do something like an OnItemSelected(). Then see what item and subitem is clicked and if it is the second sub item, isn't one of the disabled images, check or uncheck respectively.

    I've provided all the project code zipped up and the main Dialog and List Item Changed code below

    Code:
    **************** Dialog Init Code ***************
    BOOL CCheckLCDemoDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Add "About..." menu item to system menu.
    
    	// IDM_ABOUTBOX must be in the system command range.
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		CString strAboutMenu;
    		strAboutMenu.LoadString(IDS_ABOUTBOX);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    	
    	// TODO: Add extra initialization here
    	CRect rect;
    	m_listCtrl.GetClientRect(rect);
    	m_listCtrl.InsertColumn(0, _T(""), LVCFMT_CENTER, 30);
    	m_listCtrl.InsertColumn(1, _T("Layer"), LVCFMT_LEFT, 150);
    	m_listCtrl.InsertColumn(2, _T("Zoom"), LVCFMT_LEFT, 100);
    	m_listCtrl.InsertColumn(3, _T("Labels"), LVCFMT_LEFT, rect.Width() - 280);
    
    
    	VERIFY( m_checkLCImgList.Create(IDB_CHECKBOXES, 16, 4, RGB(255,0,255)) );
    	int j = m_checkLCImgList.GetImageCount();
    	m_listCtrl.SetImageList(&m_checkLCImgList, LVSIL_SMALL);
    	
    	
    	//Add items
    	int nPos = 0;
    	LV_ITEM lvi;
    
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
    	lvi.pszText = "";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iImage = 4; // First image from image list
    	nPos = m_listCtrl.InsertItem(&lvi);
    
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT;
    	lvi.pszText = "Item 1";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iItem = nPos;
    	lvi.iSubItem = 1;
    	m_listCtrl.SetItem(&lvi);
    	
    	
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
    	lvi.pszText = "";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iImage = 3; // First image from image list
    	nPos = m_listCtrl.InsertItem(&lvi);
    
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT;
    	lvi.pszText = "Item 1";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iItem = nPos;
    	lvi.iSubItem = 1;
    	m_listCtrl.SetItem(&lvi);
    	
    	
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
    	lvi.pszText = "";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iImage = 1; // First image from image list
    	nPos = m_listCtrl.InsertItem(&lvi);
    
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT;
    	lvi.pszText = "Item 1";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iItem = nPos;
    	lvi.iSubItem = 1;
    	m_listCtrl.SetItem(&lvi);
    	
    	
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
    	lvi.pszText = "";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iImage = 1; // First image from image list
    	nPos = m_listCtrl.InsertItem(&lvi);
    
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT;
    	lvi.pszText = "Item 1";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iItem = nPos;
    	lvi.iSubItem = 1;
    	m_listCtrl.SetItem(&lvi);
    	
    	
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
    	lvi.pszText = "";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iImage = 2; // First image from image list
    	nPos = m_listCtrl.InsertItem(&lvi);
    
    	ZeroMemory(&lvi, sizeof(lvi));
    	lvi.mask = LVIF_TEXT;
    	lvi.pszText = "Item 1";
    	lvi.cchTextMax = lstrlen(lvi.pszText);
    	lvi.iItem = nPos;
    	lvi.iSubItem = 1;
    	m_listCtrl.SetItem(&lvi);
    	
    	
    	int listOrder[4];
    	listOrder[0] = 1;
    	listOrder[1] = 0;
    	listOrder[2] = 2;
    	listOrder[3] = 3;
    	m_listCtrl.SetColumnOrderArray(4, listOrder);
    	
    
    	m_listCtrl.SetExtendedStyle( m_listCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES);
    	m_listCtrl.Init();
    	
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    
    void CCheckListCtrl::OnItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	NMLISTVIEW* pNMLV = (NMLISTVIEW*)pNMHDR;
    	*pResult = 0;
    
    	if ( m_blInited && LVIF_STATE == pNMLV->uChanged)
    	{
    		//Not sure on how to continue from here
    		//See what item and sub item have been selected if the image is not
    		//disabled then the active check or uncheck box images get displayed
    		//respectively.
    	}
    
    }
    Attached Images Attached Images   
    Attached Files Attached Files
    Last edited by ill_comms; March 22nd, 2009 at 09:21 PM. Reason: Added code tags

  2. #2
    Join Date
    Oct 2005
    Posts
    27

    How to tell what sub item has been clicked on?

    Hi All,

    I really need to figure out how to do this, "how to tell what sub item has been clicked on".

    Any help will be greatly appreciated
    Regards Hayden

  3. #3
    Join Date
    Oct 2005
    Posts
    27

    Re: List Control changing sub items image.

    Found the answer, decided to for the following code. If someone clicks on the sub item with the checkbox image inside it which happens to be zero (the first column) then it changes it's image accordingly, from the image list.

    void CCheckListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
    {
    CListCtrl::OnLButtonDown(nFlags, point);

    CString trial = "";

    LVHITTESTINFO hti = {0};
    hti.pt = point;
    if (SubItemHitTest(&hti) == -1)
    return;
    int iItem = hti.iItem;
    int iSubItem = hti.iSubItem;

    if (iItem == -1 || iSubItem == -1)
    return;
    if (iSubItem == 0)
    {
    //Then see what state the sub item is if disabled or enabled, then checked or unchecked
    LVITEM tempItem;
    int theImage = 0;
    CString theText = "";
    ZeroMemory(&tempItem, sizeof(tempItem));
    tempItem.mask = LVIF_IMAGE;
    tempItem.iItem = iItem;
    tempItem.iSubItem = iSubItem;
    GetItem(&tempItem);

    theImage = tempItem.iImage;
    if (theImage > 2)
    {
    //Do nothing it is disabled
    }
    else
    {
    if (theImage == 1)
    {
    theImage = 2;
    }
    else
    {
    theImage = 1;
    }
    ZeroMemory(&tempItem, sizeof(tempItem));
    tempItem.mask = LVIF_IMAGE;
    tempItem.iItem = iItem;
    tempItem.iSubItem = iSubItem;
    tempItem.iImage = theImage;
    SetItem(&tempItem);
    }
    }
    }

  4. #4
    Join Date
    Jul 2005
    Location
    E: 120.6, N: 31.3′
    Posts
    795

    Re: List Control changing sub items image.

    In my opinion, perhaps using events is better to achive. In event, you can judge if the check box in this line is pressed or not and do something. Anyway, this is just thinking of mine, it's up to you for this.

  5. #5
    Join Date
    Nov 2007
    Posts
    613

    Re: List Control changing sub items image.

    I'll tell you how to detect which item and subitem is clicked.

    First, add a control variable for your list view control:
    Code:
    CListCtrl list;
    Second, you need to modify the extended style of the list view by adding the following line to the OnInitDialog function:
    Code:
    list.SetExtendedStyle(list.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
    Third, handle the NM_Click message of the list control and write the following code in it:
    Code:
    void CTest507Dlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
      NMITEMACTIVATE* pNMItemActivate = (NMITEMACTIVATE*)  pNMHDR;
      CString s;
      int iItem = pNMItemActivate->iItem;
      if(iItem != -1)
      {
        int iSubItem = pNMItemActivate->iSubItem;
        s.Format(_T("Item=&#37;d\nSubItem=%d"), iItem, iSubItem);
        MessageBox(s);
      }
    }
    Last edited by srelu; March 24th, 2009 at 07:10 PM.

  6. #6
    Join Date
    Oct 2005
    Posts
    27

    Re: List Control changing sub items image.

    This looks interesting, a few questions. Is "CTest507Dlg" supposed to be the list class or the dialog? After that how would you create the declare the "OnNMClickList1"?

  7. #7
    Join Date
    Nov 2007
    Posts
    613

    Re: List Control changing sub items image.

    Quote Originally Posted by ill_comms View Post
    This looks interesting, a few questions. Is "CTest507Dlg" supposed to be the list class or the dialog? After that how would you create the declare the "OnNMClickList1"?
    I think the name of the class is self explanatory, it's the class of the dialog. No list view derived class is supposed to be used.

    How to add a handler for the OnClick event depends on what version of VS are you using.

    In VS6 you use the class wizard (Ctrl + W), select the class of the dialog box, then select the identifier of the list view control and finally select the click event. Finally click the Add Handler (or Add Function ?) button.

    In the other versions of the VS, make sure the dialog box is open and the list view control is selected. In the toolbar of the Properties window click the "Control Events" button (the one with a yellow flash on it), then scroll down the list to find the desired event.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center