15 мая "Исходники.РУ" отмечают 20-летие присутствия в сети!
Поздравляем всех причастных и неравнодушных с юбилеем сайта!
И огромное спасибо всем, кто был с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CListCtrl entry selections...

Reza Razavipour -- biles.com!reza_r@jabberwock.biles.com
Tuesday, July 09, 1996

VC 4.1 on NT3.5.1 Patch 4.

I am trying to select a CListCtrl entry with no luck. I have tried SetItem, 
SetItemState, setting the state as I InsertItem, no luck.

Can somebody tell how to accomplish this, preferably with a code sample.

TIA

Reza



Mitchell C. Sharp -- Mitch.Sharp@worldnet.att.net
Sunday, July 14, 1996

[Mini-digest: 5 responses]

Reza Razavipour wrote:
> 
> VC 4.1 on NT3.5.1 Patch 4.
> 
> I am trying to select a CListCtrl entry with no luck. I have tried SetItem,
> SetItemState, setting the state as I InsertItem, no luck.
> 
> Can somebody tell how to accomplish this, preferably with a code sample.
> 
> TIA
> 
> Reza

Where are you trying to do this. I use setitem and it works fine, but 
you have to be careful where you set it.
-----From: cdowns@dev.tivoli.com (Carl Downs)

Hope this helps...Carl

// CMyDialog Data
	//{{AFX_DATA(CDlgFilter)
	enum { IDD = IDD_DLG };
	CListCtrl	m_cList;
	//}}AFX_DATA


CMyDialog::OnInitDlg()
{
        // load some stuff in to the CListCtrl.
        ...

	// make the first entry selected.

	m_cList.SetItemState (0, 
		                  LVIS_SELECTED | LVIS_FOCUSED, 
		                  LVIS_SELECTED | LVIS_FOCUSED);
}

-----From: Doug Persons 

Here is the code I use on VC 4.0, NT3.5.1 p4:

m_list is a CListCtrl initialized in DoDataExchange:
    DDX_Control(pDX, IDC_FOO_LIST, m_list);

when the list was initialized, the pointer to the data object was stored in 
the lParam of the list view item.  The select method will select an item in 
the list based on this pointer value:

void
FooList::select(Foo* selection)
{
    int nextItem = -1;
    LV_ITEM lvitem;
    lvitem.mask      = LVIF_STATE;
    lvitem.state     = LVIS_SELECTED;     
    lvitem.stateMask = LVIS_SELECTED;
    lvitem.iSubItem  = 0;
    m_list.SetFocus();

    while(-1 != (nextItem = m_list.GetNextItem( nextItem, LVNI_ALL )))
    {
        if(selection == (Foo*)m_list.GetItemData(nextItem))
        {
            lvitem.iItem = nextItem;
            m_list.SetItem(&lvitem);
            break;
        }
    }
}



-- 
Doug Persons                206-822-6800    persons@esca.com
Cegelec ESCA Corporation    Bellevue, WA    http://www.esca.com
-----From: Prasad Vemuri 

Reza:
	Here is the code snippet for selecting the listctrl item.

	// I have the list control as datamember within the
	// CView derieved class.
	// The folowing finction takes the item number as
	// parameter and sets teh selection to that.
	// Call this function as soon as you insert the item...	
	void CCommentsListView::SetItemFocus(int nIndex)
	{
		UINT nState = LVIS_SELECTED | LVIS_FOCUSED;
		UINT nMask = LVIS_SELECTED | LVIS_FOCUSED | 
				LVIS_CUT| LVIS_DROPHILITED;
				//0x000F;

		// Set the selection && Update the Details view..
		if ( (m_pListControl) && (m_pListControl->GetItemCount() > 0) )
			m_pListControl->SetItemState(nIndex, nState, nMask);

	}

Hope this would help you..

Regards,
Prasad Vemuri (K.P)

******************************************************************************
Prasad V Vemuri (K.P)                    TSI VISION (Switch Interfaces)
Technical Consultant                     CSC Communications Industry Services
Voice.(217)351-8250 Ext:2293             TRIS Division
Fax.(217)351-2640                        115 North Neil  
Email: pvemuri@mars.csci.csc.com         Champaign, IL 61824-0770
*****************************************************************************


-----From: Mario Contestabile

The on-line help contains:

  
PSS ID Number: Q131284
Article last modified on 06-10-1995
 
1.30 4.00 | 3.51
 
WINDOWS   | WINDOWS NT
 

---------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Win32 Software Development Kit (SDK) versions 3.51, 4.0
 - Microsoft Win32s version 1.3
---------------------------------------------------------------------
 
SUMMARY
=======
 
Selecting a listview item in Windows 95 is not as easy as selecting a list
box item was in Windows version 3.1. To select a list box item in Windows
version 3.1, an application sends an LB_SETCURSEL or LB_SETSEL to a single-
or multiple-selection list box respectively. To select a listview item in
Windows 95, an application sends an LVM_SETITEMSTATE message or calls the
ListView_SetItemState() macro.
 
MORE INFORMATION
================
 
An application can force a selection of a listview item. You might want the
application to do this when a user clicks a column other than the first
column of a listview of multiple subitems or columns.
 
Currently, a listview item is selected only when the user clicks the first
column of that item. However, you many want the application to select the
item regardless of which column in the listview is clicked.
 
Windows 95 does not provide a separate message or function to set the
current selection in a listview. Instead, it defines item states or LVIS_*
values that determine the listview item's appearance and functionality.
LVIS_FOCUSED and LVIS_SELECTED in particular are the states that determine
a listview item's selection state.
 
To select a listview item programmatically, an application sets the
listview item's state as follows:
 
   ListView_SetItemState (hWndListView,         // handle to listview
                          iWhichItem,         // index to listview item
                          LVIS_FOCUSED | LVIS_SELECTED, // item state
                          0x000F);                      // mask
 
Note that the last parameter passed to this macro is a mask specifying
which bits are about to change. LVIS_FOCUSED and LVIS_SELECTED are defined
in  as 0x0001 and 0x0002 respectively, so you need to set the
last four bits of the mask.
 
The same principle applies to selecting a treeview item programmatically.
The only difference is that an application sends a TVM_SETITEM message or
calls the TreeView_SetItem() macro.
 
Because listviews allow multiple selection by default, you can program an
application to select multiple items by simulating a CTRL keydown (or SHIFT
keydown event) prior to setting the item state. For example, the following
code simulates the pressing of the CTRL key:
 
   BYTE  pbKeyState [256];
 
   GetKeyboardState ((LPBYTE)&pbKeyState);
   pbKeyState[VK_CONTROL] |= 0x80;
   SetKeyboardState ((LPBYTE)&pbKeyState);
 
Note that if an application simulates a keypress, it must also be
responsible for releasing it by resetting the appropriate bit. For example,
the following code simulates the release of a CTRL key:
 
   BYTE  pbKeyState [256];
 
   GetKeyboardState ((LPBYTE)&pbKeyState);
   pbKeyState[VK_CONTROL] = 0;
   SetKeyboardState ((LPBYTE)&pbKeyState);
 
Similarly, retrieving the currently selected item in a listview control
in Windows 95 is not as easy as sending an LB_GETCURSEL message to a
listbox control was in Windows version 3.1.
 
For listviews, call the ListView_GetNextItem() function with the
LVNI_SELECTED flag specified:
 
   iCurSel = ListView_GetNextItem (ghwndLV, -1, LVNI_SELECTED);
 
For treeviews, retrieve the currently selected item by calling the
TreeView_GetNextItem() function  with the TVGN_CARET flag
specified or by calling the TreeView_GetSelection() macro directly:
 
   iCurSel = TreeView_GetNextItem (ghwndTV, NULL, TVGN_CARET);
      or
   iCurSel = TreeView_GetSelection (ghwndTV);
 
Additional reference words: 4.00 1.30
KBCategory: kbprg kbcode
KBSubcategory: UsrCtl
 
=============================================================================
 
Copyright Microsoft Corporation 1995.





| Вернуться в корень Архива |