CListCtrl selection
Svetlana Guljajeva -- svetlana@assert.ee Thursday, February 20, 1997 Environment : Visual C++ 4.2-flat,Windows NT 4.0 Hello! I am trying to set selection in list control ( CListCtrl derivative) programmatically. When user clicks on any place of the item (not only on the leftmost column),program should select the item that was clicked. I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): { ... UINT uFlags = 0; int itemHit = HitTest(point,&uflags); if(itemHit != -1) { LV_ITEM item; item.mask = LVIF_STATE; item.iItem = itemHit; item.stateMask = LVIS_SELECTED; item.state = 0xFFFF; //I tried also this way -->> item.state = 1; SetItem(&item); } } itemHit is always right,but after executing these lines of code,nothing is selected. What's wrong in my code? Thanks in advance. ----------------------------------------------------------------- Svetlana Guljajeva / Assert Ltd. / Tallinn Email : svetlana@assert.ee Web page : http://galaxy.assert.ee -----------------------------------------------------------------
Doug Persons -- persons@esca.com Thursday, February 20, 1997 [Mini-digest: 9 responses] Have you tried: =20 SetItemState(itemHit, LVIS_SELECTED, LVIS_SELECTED); Doug Persons 206-822-6800 persons@esca.com Cegelec ESCA Corporation Bellevue, WA http://www.esca.com ---------- > I am trying to set selection in list control ( CListCtrl derivative) > programmatically. > When user clicks on any place of the item (not only on the leftmost > column),program should select the item that was clicked. > I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl)= : >=20 > { > ... > UINT uFlags =3D 0; > int itemHit =3D HitTest(point,&uflags); > if(itemHit !=3D -1) > { > LV_ITEM item; > item.mask =3D LVIF_STATE; > item.iItem =3D itemHit; > item.stateMask =3D LVIS_SELECTED; > item.state =3D 0xFFFF; > //I tried also this way -->> item.state =3D 1; > SetItem(&item); > } > } >=20 > itemHit is always right,but after executing these lines of code,nothing is > selected. > What's wrong in my code? -----From: Mike BlaszczakAt 16:06 2/20/97 +0200, Svetlana Guljajeva wrote: >Environment : Visual C++ 4.2-flat,Windows NT 4.0 You should upgrade to MFC 4.2b by installing the patch. 4.2-flat only works reliably against beta versions of certain operating system components, and will cause you trouble sooner or later. >I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): CListCtrlEx is probably a bad name for a class--you should expect that MFC itself will use that name someday. >{ > ... Are you calling the base class handler for the function? > item.stateMask =3D LVIS_SELECTED; > item.state =3D 0xFFFF; >//I tried also this way -->> item.state =3D 1; You should be using the state flag for the state member: item.stateMask =3D LVIS_SELECTED; item.state =3D LVIS_SELECTED; Are you doing anything in response to NM_CLICK? .B ekiM http://www.nwlink.com/~mikeblas/ These words are my own. I do not speak on behalf of Microsoft. This performance was not lip-synched. -----From: kevin@pln.com.tw (Kevin Tarn) Try this, if(itemHit !=3D -1) { LV_ITEM item; item.mask =3D LVIF_STATE; item.iItem =3D itemHit; GetItem(&item); item.stateMask |=3D LVIS_SELECTED; item.state |=3D LVIS_SELECTED; SetItem(&item); } Kevin Tarn -----Original Message----- From: Svetlana Guljajeva [SMTP:svetlana@assert.ee] Sent: Thursday, February 20, 1997 10:07 PM To: MFC list Subject: CListCtrl selection Environment : Visual C++ 4.2-flat,Windows NT 4.0 Hello! I am trying to set selection in list control ( CListCtrl derivative) programmatically. When user clicks on any place of the item (not only on the leftmost column),program should select the item that was clicked. I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): { ... UINT uFlags =3D 0; int itemHit =3D HitTest(point,&uflags); if(itemHit !=3D -1) { LV_ITEM item; item.mask =3D LVIF_STATE; item.iItem =3D itemHit; item.stateMask =3D LVIS_SELECTED; item.state =3D 0xFFFF; //I tried also this way -->> item.state =3D 1; SetItem(&item); } } itemHit is always right,but after executing these lines of code,nothing i= s selected. What's wrong in my code? Thanks in advance. ----------------------------------------------------------------- Svetlana Guljajeva / Assert Ltd. / Tallinn Email : svetlana@assert.ee Web page : http://galaxy.assert.ee ----------------------------------------------------------------- -----From: Michael Schneider Hello Svetlana, > I am trying to set selection in list control ( CListCtrl derivative) > programmatically. > When user clicks on any place of the item (not only on the leftmost > column),program should select the item that was clicked. > I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl)= : > { > ... > UINT uFlags =3D3D 0; > int itemHit =3D3D HitTest(point,&uflags); > if(itemHit !=3D3D -1) > { > LV_ITEM item; > item.mask =3D3D LVIF_STATE; > item.iItem =3D3D itemHit; > item.stateMask =3D3D LVIS_SELECTED; > item.state =3D3D 0xFFFF; > //I tried also this way -->> item.state =3D3D 1; You should change this line to item.state =3D3D LVIS_SELECTED; because stateMask holds the states you want to change and state holds the= =3D new value of this states. For example if stateMask is LVIS_SELECTED | LVI= S=3D _FOCUSED and state ist LVIS_SELECTED that means, that your item is select= e=3D d, but the focus is removed. > SetItem(&item); > } > } > itemHit is always right,but after executing these lines of code,nothing= =3D is > selected. > What's wrong in my code? After changing an items state you should call Update( itemHit) to force t= h=3D e list control to redraw this item. > Thanks in advance. > ----------------------------------------------------------------- > Svetlana Guljajeva / Assert Ltd. / Tallinn > Email : svetlana@assert.ee > Web page : http://galaxy.assert.ee > ----------------------------------------------------------------- I hope this helps. Mike =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D= 3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D= 3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D Michael Schneider Pirckheimer Weg 13 91058 Erlangen Germany Phone: ++49 9131 771539 E-Mail: Mike.Schneider@fen.baynet.de Mike.Schneider@t-online.de WWW: http://home.t-online.de/home/Mike.Schneider =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D= 3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D= 3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D -----From: Syed At 04:06 PM 2/20/97 +0200, you wrote: >Environment : Visual C++ 4.2-flat,Windows NT 4.0 > >Hello! > >I am trying to set selection in list control ( CListCtrl derivative) >programmatically. >When user clicks on any place of the item (not only on the leftmost >column),program should select the item that was clicked. >I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): > >{ > ... > UINT uFlags =3D 0; > int itemHit =3D HitTest(point,&uflags); > if(itemHit !=3D -1) > { > LV_ITEM item; > item.mask =3D LVIF_STATE; > item.iItem =3D itemHit; > item.stateMask =3D LVIS_SELECTED; > item.state =3D 0xFFFF; >//I tried also this way -->> item.state =3D 1; > SetItem(&item); > } >} > >itemHit is always right,but after executing these lines of code,nothing = is >selected. >What's wrong in my code? > >Thanks in advance. There are two reasons that I can think of:- 1)It _may_ be that you don't initialize item.iSubItem. Usually, it's set = to 0. 2)The ListCtrl is not the active view or does not receive currently recei= ve focus. There are two ways to solve it:- 1)Add the style LVIS_SHOWSELALWAYS. Looks at the documentation on how th= is affect selection in your code. (Thanks to David Elliot, who pointed this = out earlier to me.) 2)SetFocus before begin selecting item (not sure whether this really hel= ps - don't bother putting it since the first method is fine). One more, if what you want to accomplish is simple select an item, why do= n't you use the cleaner and simpler SetItemState. For example:- SetItemState(itemHit, 0xffff, LVIS_SELECTED); p/s: I'm quite lucky today. I was about to repost a question (was rejecte= d :) ) on almost exactly the same thing (selecting items programmatically i= n CListView), when I read your mail. I notice that you put 0xffff instead o= f 1 in newState parameter (which solve my problem). Why is that..? -----From: "Serge Wautier" Hi Svetlana, I'm amazed iItemHit is right. I want the same behaviour for my CListCtrl and, as far as i remember, the hittest'ed point has to be on the leftmost subitem. Anyway, here's a code snippet from my apps : void CMyList::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) // NM_CLICK reflected message { ... pt.x=3D4; // To be sure the point is on the leftmost subitem int Index=3DHitTest(pt); if (Index=3D=3D-1) return; =20 SetItemState(Index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED)= ; } Hope this helps. Serge Wautier, Techno Trade s.a. Belgium serge.wautier@ontonet.be http://www.tbox.fr ---------- > From: Svetlana Guljajeva > To: MFC list > Subject: CListCtrl selection > Date: jeudi 20 f=E9vrier 1997 15:06 >=20 > Environment : Visual C++ 4.2-flat,Windows NT 4.0 >=20 > Hello! >=20 > I am trying to set selection in list control ( CListCtrl derivative) > programmatically. > When user clicks on any place of the item (not only on the leftmost > column),program should select the item that was clicked. > I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl)= : >=20 > { > ... > UINT uFlags =3D 0; > int itemHit =3D HitTest(point,&uflags); > if(itemHit !=3D -1) > { > LV_ITEM item; > item.mask =3D LVIF_STATE; > item.iItem =3D itemHit; > item.stateMask =3D LVIS_SELECTED; > item.state =3D 0xFFFF; > //I tried also this way -->> item.state =3D 1; > SetItem(&item); > } > } >=20 > itemHit is always right,but after executing these lines of code,nothing is > selected. > What's wrong in my code? >=20 > Thanks in advance. >=20 > ----------------------------------------------------------------- > Svetlana Guljajeva / Assert Ltd. / Tallinn > Email : svetlana@assert.ee > Web page : http://galaxy.assert.ee > ----------------------------------------------------------------- -----From: "No=EBl DANJOU" Hi Svetlana, You can search the knowledge base (http://www.microsoft.com/kb/) for arti= cle=20 Q131788. "Highlighting an Entire Row in a ListView Control" it's about=20 ListView but it's quite the same for ListCtrl. (this code is C/SDK not MF= C :=20 if you want a MFC sample, email me and I will send you a part of a source= code=20 from one application where I had to do that in MFC.) The basic idea of this feature is to make your control owner-draw and mak= e the=20 drawing yourself.=20 If I remember well I think I read this feature will be supported (through= an=20 additionnal flag) in the common controls in IE4 aka Nashville.=20 HTH Noel =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D Noel DANJOU 12/21/1968 Village Paillette Software Engineer @ PalmCom 50320 Saint Jean des Champs Email: noeld@msn.com France UIN : 187712 NetMeeting 2.0 B2: callto:uls.microsoft.com/noeld@msn.com Home Pages: http://ourworld.compuserve.com/homepages/noeld/ (personal) http://ourworld.compuserve.com/homepages/gil_mic/ (company) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D ---------- De: owner-mfc-l@majordomo.netcom.com de la part deSvetlana Guljajeva Date d'envoi: jeudi 20 f=E9vrier 1997 15:07 A: MFC list Objet: CListCtrl selection Environment : Visual C++ 4.2-flat,Windows NT 4.0 Hello! I am trying to set selection in list control ( CListCtrl derivative) programmatically. When user clicks on any place of the item (not only on the leftmost column),program should select the item that was clicked. I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): { ... UINT uFlags =3D 0; int itemHit =3D HitTest(point,&uflags); if(itemHit !=3D -1) { LV_ITEM item; item.mask =3D LVIF_STATE; item.iItem =3D itemHit; item.stateMask =3D LVIS_SELECTED; item.state =3D 0xFFFF; //I tried also this way -->> item.state =3D 1; SetItem(&item); } } itemHit is always right,but after executing these lines of code,nothing i= s selected. What's wrong in my code? Thanks in advance. ----------------------------------------------------------------- Svetlana Guljajeva / Assert Ltd. / Tallinn Email : svetlana@assert.ee Web page : http://galaxy.assert.ee ----------------------------------------------------------------- -----From: Mario Contestabile >itemHit is always right,but after executing these lines of code,nothing = is >selected. >What's wrong in my code? try=20 Select(item,TVGN_DROPHILITE); mcontest@universal.com -----From: Regis NICOLAS At 04:06 PM 2/20/97 +0200, you wrote: >Environment : Visual C++ 4.2-flat,Windows NT 4.0 > >Hello! > >I am trying to set selection in list control ( CListCtrl derivative) >programmatically. >When user clicks on any place of the item (not only on the leftmost >column),program should select the item that was clicked. >I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): > >{ > ... > UINT uFlags =3D 0; > int itemHit =3D HitTest(point,&uflags); > if(itemHit !=3D -1) > { > LV_ITEM item; > item.mask =3D LVIF_STATE; > item.iItem =3D itemHit; > item.stateMask =3D LVIS_SELECTED; > item.state =3D 0xFFFF; >//I tried also this way -->> item.state =3D 1; > SetItem(&item); > } >} > >itemHit is always right,but after executing these lines of code,nothing = is >selected. >What's wrong in my code? > >Thanks in advance. > >----------------------------------------------------------------- >Svetlana Guljajeva / Assert Ltd. / Tallinn >Email : svetlana@assert.ee >Web page : http://galaxy.assert.ee >----------------------------------------------------------------- > > I tried your code but it does not work: itemHit is always -1. I wrote a piece of code for you: /////////////////////////////////////////////////////////////////////////= //// class CListCtrlEx : public CListCtrl { public: afx_msg void OnLButtonDown( UINT nFlags, CPoint point ); protected: DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl) ON_WM_LBUTTONDOWN() END_MESSAGE_MAP() void CListCtrlEx::OnLButtonDown( UINT nFlags, CPoint point ) { // Use if (GetStyle() & LVS_REPORT) if want want it works for List mode A= ND Report mode if ((GetStyle() & LVS_TYPEMASK) =3D=3D LVS_REPORT) { CRect theRect; // Find the Rect around the label and force the point as = if it was clicked in this rect GetItemRect(0, theRect, LVIR_LABEL); point.x =3D theRect.left; CListCtrl::OnLButtonDown( nFlags, point ); UINT uFlags =3D 0; int itemHit =3D HitTest(point,&uFlags); // Directly use SetItemState is better... if(itemHit !=3D -1) SetItemState(itemHit, LVIS_SELECTED | GetItemState(itemHit, 0xFFFF), 0= xFFFF); } else Default(); } /////////////////////////////////////////////////////////////////////////= //// BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // Just add 2 cols and 2 items in the List CListCtrlEx* theList =3D new CListCtrlEx; theList->SubclassWindow(GetDlgItem(IDC_LIST)->GetSafeHwnd()); =09 LV_COLUMN theLVC; theLVC.mask =3D LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH; theLVC.fmt =3D LVCFMT_LEFT; theLVC.cx =3D 100; =09 theLVC.iSubItem =3D 0; theLVC.pszText =3D "Column 1"; theList->InsertColumn(0, &theLVC); theLVC.iSubItem =3D 1; theLVC.pszText =3D "Column 2"; theList->InsertColumn(1, &theLVC); int theItem; theItem =3D theList->InsertItem(0, "Item1"); theList->SetItem(theItem, 1, LVIF_TEXT, "SubItem", 0, 0, 0, 0); theItem =3D theList->InsertItem(1, "Item2"); theList->SetItem(theItem, 1, LVIF_TEXT, "SubItem", 0, 0, 0, 0); =09 // TODO: Add extra initialization here =09 return TRUE; // return TRUE unless you set the focus to a control } By the way: this will not work for right button click and for Context Men= us. In the new common controls (ActiveX SDK) you can use the LVS_EX_FULLROWSELECT style to do it... Hope this helps... ------------------------------ Regis NICOLAS - R&D Windows Smartcode Technologie mailto:nicolas@smartcode.fr http://www.smartcode.fr/ http://www.smartcodesoft.com/ Tel.: (33) (0)4 67 59 30 16
Shane Triem -- shane@steptech.com Monday, February 24, 1997 If this is the same CListCtrlEx that is available from MSDN, then when you first configure the control in the parent class, use the member function YourCListCtrlEx::SetFullRowSel(TRUE); This will select a row (and paint it as selected) whenever the user clicks anywhere on a row (provided you are using the CListCtrl in report mode). Shane STEP Technology Portland, OR shane@steptech.com ---------- From: Svetlana Guljajeva Sent: Friday, February 21, 1997 1:29 AM To: SHANE; "MFC list" Subject: CListCtrl selection Environment : Visual C++ 4.2-flat,Windows NT 4.0 Hello! I am trying to set selection in list control ( CListCtrl derivative) programmatically. When user clicks on any place of the item (not only on the leftmost column),program should select the item that was clicked. I handle WM_LBUTTONDOWN message in CListCtrlEx (derived from CListCtrl): { ... UINT uFlags = 0; int itemHit = HitTest(point,&uflags); if(itemHit != -1) { LV_ITEM item; item.mask = LVIF_STATE; item.iItem = itemHit; item.stateMask = LVIS_SELECTED; item.state = 0xFFFF; //I tried also this way -->> item.state = 1; SetItem(&item); } } itemHit is always right,but after executing these lines of code,nothing is selected. What's wrong in my code? Thanks in advance. ----------------------------------------------------------------- Svetlana Guljajeva / Assert Ltd. / Tallinn Email : svetlana@assert.ee Web page : http://galaxy.assert.ee -----------------------------------------------------------------
Parameswaran.Ramaswamy@blr.siemens.de Thursday, March 06, 1997 Environment : VC++ 4.0 Win 95 Hi, I have a serious problem with CListCtrl. I have a list control with items and subitems. When the user presses any alphabet, I would like to take the selection to the item containing the pressed alphabet as the first character. This is taken care of automatically by CListCtrl. But I would like to get the index of the item. I tried capturing WM_KEYDOWN but I get only the code of the key. I am not able to find any function in CListCtrl similiar to GetCursel in CListBox. I tried doing it indirectly but to no avail. Anybody know the solution, please help me. Rams
Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com Thursday, March 06, 1997 [Mini-digest: 18 responses] > But I would >like to get the index of the item. I tried capturing WM_KEYDOWN but I get >only the code of the key. I am not able to find any function in CListCtrl >similiar to GetCursel in CListBox. I tried doing it indirectly but to no >avail. Anybody know the solution, please help me. Find *first* selected, retrieve column 0 text const int index = m_ListCtrl.GetNextItem(-1, LVNI_SELECTED); if(-1 != index) return m_ListCtrl.GetItemText(index, 0); else return _T(""); mcontest@universal.com -----From: Paul GerhartParameswaran.Ramaswamy@blr.siemens.de wrote: > > Environment : VC++ 4.0 Win 95 > > Hi, > I have a serious problem with CListCtrl. I have a list control > with items and subitems. When the user presses any alphabet, I would like to > take the selection to the item containing the pressed alphabet as the first > character. This is taken care of automatically by CListCtrl. But I would > like to get the index of the item. I tried capturing WM_KEYDOWN but I get > only the code of the key. I am not able to find any function in CListCtrl > similiar to GetCursel in CListBox. I tried doing it indirectly but to no > avail. Anybody know the solution, please help me. > > Rams > I did something on these lines. I made the control owner-draw. Then, when I added items to the control I put an index number in the ItemData member. Then I made a handler for DrawItem (it is owner-draw) - this handler is passed the value of ItemData (its part of the DRAWITEM structure). That is how I knew which row was being selected. I have a freeware source example of this at http://www.voicenet.com/~pgerhart/_shware.html Look for the app called GridList. Hope this helped. -Paul -- ######################### # Paul Gerhart # # pgerhart@voicenet.com # ######################### -----From: spencer@cix.compulink.co.uk (Steve Spencer) In-Reply-To: On 6th March, Rams wrote > I have a serious problem with CListCtrl. I have a list control > with items and subitems. When the user presses any alphabet, I would > like to take the selection to the item containing the pressed alphabet > as the first character. This is taken care of automatically by > CListCtrl. But I would like to get the index of the item. I tried > capturing WM_KEYDOWN but I get only the code of the key. I am not able > to find any function in CListCtrl similiar to GetCursel in CListBox. I > tried doing it indirectly but to no avail. Anybody know the solution, > please help me. > > Rams > Maybe you need to trap WM_CHAR, not WM_KEYDOWN, for the list control. In addition, if you call the CListCtrl version first, you should then be able to find the current selection by using the sequence; GetNextItem(-1, LVNI_SELECTED); (Thanks to Mike B for pointing it out in his book!) This should then do what you want. Steve Spencer spencer@cix.co.uk MFC Developer/Trainer -----From: Mike Blaszczak At 09:52 3/6/97 ITZ, Parameswaran.Ramaswamy@blr.siemens.de wrote: >Environment : VC++ 4.0 Win 95 > I have a serious problem with CListCtrl. I have a list control >with items and subitems. When the user presses any alphabet, I would like to >take the selection to the item containing the pressed alphabet as the first >character. This is taken care of automatically by CListCtrl. But I would >like to get the index of the item. I tried capturing WM_KEYDOWN but I get >only the code of the key. You should catch the LVN_ITEMCHANGED message. You'll get one message for the item that's become unselected, and one for the item that's newly selected. >I am not able to find any function in CListCtrl >similiar to GetCursel in CListBox. You can call CListCtrl::GetNextItem() with the LVNI_SELECTED flag. >I tried doing it indirectly but to no avail. What does that mean, specifically? .B ekiM http://www.nwlink.com/~mikeblas/ These words are my own. I do not speak on behalf of Microsoft. This performance was not lip-synched. -----From: "Dong Chen" Check the on-line help for the GetNextItem() function. Use the LVNI_SELECTED flag, you should be able to do it. -- Dong d_chen@ix.netcom.com ---------- > From: Parameswaran.Ramaswamy@blr.siemens.de > To: mfc-l@netcom.com > Subject: CListCtrl Selection > Date: Thursday, March 06, 1997 3:52 AM > > Environment : VC++ 4.0 Win 95 > > Hi, > I have a serious problem with CListCtrl. I have a list control > with items and subitems. When the user presses any alphabet, I would like to > take the selection to the item containing the pressed alphabet as the first > character. This is taken care of automatically by CListCtrl. But I would > like to get the index of the item. I tried capturing WM_KEYDOWN but I get > only the code of the key. I am not able to find any function in CListCtrl > similiar to GetCursel in CListBox. I tried doing it indirectly but to no > avail. Anybody know the solution, please help me. > > Rams > -----From: Roma Parameswaran.Ramaswamy@blr.siemens.de wrote: > > Environment : VC++ 4.0 Win 95 > [snip] > ... I am not able to find any function in CListCtrl > similiar to GetCursel in CListBox. I tried doing it indirectly but to no > avail. Anybody know the solution, please help me. This function is int GetNextItem( int nItem, int nFlags ); if you want to find selected item in the single selection listcontrol, then: int index = list.GetNextItem(-1, LVNI_SELECTED); -Roma -----From: "Alian Ltd." --- Cut from the MFC CListCtrl Documentation. Title: = CListCtrl::GetNextItem --- int GetNextItem( int nItem, int nFlags ) const; =20 Return Value The index of the next item if successful, or -1 otherwise. Parameters nItem Index of the item to begin the searching with, or -1 to find the = first item that matches the specified flags. The specified item itself = is excluded from the search. nFlags Geometric relation of the requested item to the specified item, = and the state of the requested item. The geometric relation can be one = of these values: =B7 LVNI_ABOVE Searches for an item that is above the specified item. =B7 LVNI_ALL Searches for a subsequent item by index (the default = value). =B7 LVNI_BELOW Searches for an item that is below the specified item. =B7 LVNI_TOLEFT Searches for an item to the left of the specified = item. =B7 LVNI_TORIGHT Searches for an item to the right of the specified = item. The state can be zero, or it can be one or more of these values: =B7 LVNI_DROPHILITED The item has the LVIS_DROPHILITED state flag set. =B7 LVNI_FOCUSED The item has the LVIS_FOCUSED state flag set. =B7 LVNI_HIDDEN The item has the LVIS_HIDDEN state flag set. =B7 LVNI_MARKED The item has the LVIS_MARKED state flag set. =B7 LVNI_SELECTED The item has the LVIS_SELECTED state flag set. If an item does not have all of the specified state flags set, the = search continues with the next item. Remarks Searches for a list view item that has the specified properties and that = bears the specified relationship to a given item. --- end of cut --- Use either LVNI_FOCUSED (this is where the 'cursor' is), or = LVNI_SELECTED (this is item, which is selected - be careful - there = might be many selected items, if your CListCtrl allows multiple = selection) Wishing success ! Dony ---------- From: = Parameswaran.Ramaswamy@blr.siemens.de[SMTP:Parameswaran.Ramaswamy@blr.sie= mens.de] Sent: Thursday, March 06, 1997 11:52 AM To: mfc-l@netcom.com Subject: CListCtrl Selection Environment : VC++ 4.0 Win 95 Hi, I have a serious problem with CListCtrl. I have a list control=20 with items and subitems. When the user presses any alphabet, I would = like to=20 take the selection to the item containing the pressed alphabet as the = first=20 character. This is taken care of automatically by CListCtrl. But I = would=20 like to get the index of the item. I tried capturing WM_KEYDOWN but I = get=20 only the code of the key. I am not able to find any function in = CListCtrl=20 similiar to GetCursel in CListBox. I tried doing it indirectly but to = no=20 avail. Anybody know the solution, please help me. Rams =09 -----From: Regis NICOLAS At 09:52 AM 3/6/97 ITZ, you wrote: >Environment : VC++ 4.0 Win 95 > >Hi, > I have a serious problem with CListCtrl. I have a list control >with items and subitems. When the user presses any alphabet, I would like to >take the selection to the item containing the pressed alphabet as the first >character. This is taken care of automatically by CListCtrl. But I would >like to get the index of the item. I tried capturing WM_KEYDOWN but I get >only the code of the key. I am not able to find any function in CListCtrl >similiar to GetCursel in CListBox. I tried doing it indirectly but to no >avail. Anybody know the solution, please help me. > >Rams > > > Try MyList.GetNextItem(-1, LVNI_SELECTED); Hope this helps, Regis ------------------------------ Regis NICOLAS - R&D Windows Smartcode Technologie mailto:nicolas@smartcode.fr http://www.smartcode.fr/ http://www.smartcodesoft.com/ Tel.: (33) (0)4 67 59 30 16 -----From: "Knut Lamvik" CListBox: int index = m_ctrlListBox.GetCurSel(); CListCtrl: int index = m_ctrlListControl.GetNextItem(-1, LVNI_SELECTED); Definitely a 'secret' way of doing a common thing, it took me an hour to figure that out yesterday... Hope this helps, -Knut ---------- > From: Parameswaran.Ramaswamy@blr.siemens.de > To: mfc-l@netcom.com > Subject: CListCtrl Selection > Date: 6. mars 1997 10:52 > > Environment : VC++ 4.0 Win 95 > > Hi, > I have a serious problem with CListCtrl. I have a list control > with items and subitems. When the user presses any alphabet, I would like to > take the selection to the item containing the pressed alphabet as the first > character. This is taken care of automatically by CListCtrl. But I would > like to get the index of the item. I tried capturing WM_KEYDOWN but I get > only the code of the key. I am not able to find any function in CListCtrl > similiar to GetCursel in CListBox. I tried doing it indirectly but to no > avail. Anybody know the solution, please help me. > > Rams > -----From: "Gunnar \Vrn Rafnsson" Rams The following gives the index of currently selected item /////////////////////// CListCtrl lvList; =20 int nCurrSel =3D lvList.GetNextItem(-1, LVNI_SELECTED); /////////////////////// Gunnar =D6rn Rafnsson Taugagreining hf. Armuli 7b, Reykjavik, Iceland http://www.nervus.is/gor/ -----Original Message----- From: Parameswaran.Ramaswamy@blr.siemens.de = [SMTP:Parameswaran.Ramaswamy@blr.siemens.de] Sent: 7. mars 1997 13:20 To: mfc-l@netcom.com Subject: CListCtrl Selection Environment : VC++ 4.0 Win 95 Hi, I have a serious problem with CListCtrl. I have a list control=20 with items and subitems. When the user presses any alphabet, I would = like to=20 take the selection to the item containing the pressed alphabet as the = first=20 character. This is taken care of automatically by CListCtrl. But I = would=20 like to get the index of the item. I tried capturing WM_KEYDOWN but I = get=20 only the code of the key. I am not able to find any function in = CListCtrl=20 similiar to GetCursel in CListBox. I tried doing it indirectly but to = no=20 avail. Anybody know the solution, please help me. Rams -----From: ktm@ormec.com On MFC-L, Parameswaran.Ramaswamy@blr.siemens.de wrote: > When the user presses any alphabet, I would like to > take the selection to the item containing the pressed alphabet as the first > character. This is taken care of automatically by CListCtrl. But I would > like to get the index of the item. If m_ListCtrl is your list control, you can use int index = m_ListCtrl.GetNextItem(-1, LVNI_SELECTED); to find the index of the selected item. index will be -1 if no items are selected in the list. There's an example of using this function in the ROWLIST sample, \msdev\samples\mfc\GENERAL\ROWLIST Katy -- Katy Mulvey <mailto:ktm@ormec.com> Software Development Engineer ORMEC Systems -----From: "Ed Lenox" try this int item = theListCtrl.GetNextItem(-1, LVNI_SELECTED); ___________________________ Ed Lenox-Conyngham Powercom-20000 Ireland edlenox@indigo.ie ---------- > From: Parameswaran.Ramaswamy@blr.siemens.de > To: mfc-l@netcom.com > Subject: CListCtrl Selection > Date: Thursday, March 06, 1997 9:52 AM > > Environment : VC++ 4.0 Win 95 > > Hi, > I have a serious problem with CListCtrl. I have a list control > with items and subitems. When the user presses any alphabet, I would like to > take the selection to the item containing the pressed alphabet as the first > character. This is taken care of automatically by CListCtrl. But I would > like to get the index of the item. I tried capturing WM_KEYDOWN but I get > only the code of the key. I am not able to find any function in CListCtrl > similiar to GetCursel in CListBox. I tried doing it indirectly but to no > avail. Anybody know the solution, please help me. > > Rams > -----From: "Eric Rosenquist" On 6 Mar 97 at 9:52, Parameswaran.Ramaswamy@blr.si wrote: > Environment : VC++ 4.0 Win 95 > > Hi, > I have a serious problem with CListCtrl. I have a list control > with items and subitems. When the user presses any alphabet, I would like to > take the selection to the item containing the pressed alphabet as the first > character. This is taken care of automatically by CListCtrl. But I would > like to get the index of the item. I tried capturing WM_KEYDOWN but I get > only the code of the key. I am not able to find any function in CListCtrl > similiar to GetCursel in CListBox. I tried doing it indirectly but to no > avail. Anybody know the solution, please help me. You'd be better off handling LVN_KEYDOWN since that notification comes from the list control. You can handle LVN_ITEMCHANGED to find out when an item is becoming selected, though you won't know if it's due to a mouse or keyboard action. I'd try catching the LVN_KEYDOWN, giving it to the list to let it do its thing, then doing listCtrl.GetNextItem(-1, LVIS_SELECTED) to find the first selected item. Eric --------------------------------------------------------------------- Eric Rosenquist, Strata Software Limited http://www.strataware.com/ mailto:rosenqui@strataware.com Tel: 613-591-1922 Fax: 613-591-3485 Quote: I can't fake an interest in this, and I'm an expert at faking an interest in your kooky projects. -- Homer to Marge --------------------------------------------------------------------- -----From: tiger@flynet.de Hi Rams, you can do this by handling the =LVN_ITEMCHANGED Message void CYourList::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult) { NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; // TODO: Add your control notification handler code here int selection; if(pNMListView->uNewState & LVIS_SELECTED) { selection = pNMListView->iItem; .... .... } } Hope this helps. /////////////////////////////////////// // // // Frank Leuenberger // // tiger@flynet.de // // // /////////////////////////////////////// -----From: Shane Triem Cycle through the list checking the state of each item for LVIS_SELECTED Keep a running index as you cycle through the list. Shane Triem STEP Technology Portland, OR shane@steptech.com ---------- From: 'PARAMESW@SMTP ' Sent: Friday, March 07, 1997 12:10 AM To: SHANE; 'MFC-L@SMTP ' Subject: CListCtrl Selection Environment : VC++ 4.0 Win 95 Hi, I have a serious problem with CListCtrl. I have a list control with items and subitems. When the user presses any alphabet, I would like to take the selection to the item containing the pressed alphabet as the first character. This is taken care of automatically by CListCtrl. But I would like to get the index of the item. I tried capturing WM_KEYDOWN but I get only the code of the key. I am not able to find any function in CListCtrl similiar to GetCursel in CListBox. I tried doing it indirectly but to no avail. Anybody know the solution, please help me. Rams -----From: Harsh_Sharma@gw2k.com Unfortunately you do not have any function like GetCurSel() in CListCtrl. You have to iterate through each items in your ClistCtrl to find out the selected item Here is the code which will do this for you int iItems = ListCtrl.GetItemCount(); int iIndex = 0; while(iIndex < iItems) { if (ListCtrl.GetItemState(iIndex,LVIS_SELECTED) == LVIS_SELECTED) { // The current value of index is your slected Item index } } Regards Harsh Sharma Gateway 2000 Original text: (17 lines follow) From: owner-mfc-l@majordomo.netcom.com, on 3/6/97 9:52 AM: Environment : VC++ 4.0 Win 95 Hi, I have a serious problem with CListCtrl. I have a list control with items and subitems. When the user presses any alphabet, I would like to take the selection to the item containing the pressed alphabet as the first character. This is taken care of automatically by CListCtrl. But I would like to get the index of the item. I tried capturing WM_KEYDOWN but I get only the code of the key. I am not able to find any function in CListCtrl similiar to GetCursel in CListBox. I tried doing it indirectly but to no avail. Anybody know the solution, please help me. Rams -----From: Raja Segar At 09:52 06/03/1997 ITZ, you wrote: >Environment : VC++ 4.0 Win 95 > >Hi, > I have a serious problem with CListCtrl. I have a list control >with items and subitems. When the user presses any alphabet, I would like to >take the selection to the item containing the pressed alphabet as the first >character. This is taken care of automatically by CListCtrl. But I would >like to get the index of the item. I tried capturing WM_KEYDOWN but I get >only the code of the key. I am not able to find any function in CListCtrl >similiar to GetCursel in CListBox. I tried doing it indirectly but to no >avail. Anybody know the solution, please help me. > >Rams > > > The code below should do the trick. It will return all the selected items one at a time. int y = -1; while ( (y = m_ListCtrl->GetNextItem (y, LVNI_SELECTED )) >=0 ) { pItem->iItem = y; // The index of the selected item m_ListCtrl->GetItem(pItem); } ( _ \/ __)(_ ) ) /\__ \ / /_ (_)\_)(___/(____)@pc.jaring.my -----From: ASalem_Ideal@nets.com.jo (Asila Salem) Hello You can get the index of the first selected record like this. CListCtrl& lcTheList; int nStart = -1, nFirstSelected; nFirstSelected = ::SendMessage((lcTheList), LVM_GETNEXTITEM, (WPARAM), nStart , MAKELPARAM((LVIS_SELECTED|LVNI_BELOW), 0); This looks for the first selected record. You can use other values for nStart to specify where you want the search to start from, other flags with or instead of LVIS_SELECTED, and other search direction instead of LVNI_BELOW. See the VC++ online help on LVM_GETNEXTITEM. Asila Salem Senior Software Developer IdealSoft, Jordan ASalem_Ideal@nets.com.jo
Become an MFC-L member | Вернуться в корень Архива |