CListView::OnSelectAll
Kevin Bryan -- kbryan@geocities.com Friday, March 28, 1997 Environment: MSVC++ 4.0, Windows 95 I'm trying to select all of the items in my CListView-AppWizard-derived-Window. Here is the code I'm using: void CServerView::OnEditSelectAll() { CListCtrl& ctrlr = GetListCtrl(); LV_ITEM item; for (int cnt = ctrlr.GetItemCount(); cnt >= 0; cnt--) { item.iItem = cnt; item.iSubItem = 0; item.mask = LVIF_STATE; item.stateMask =0; if (!(ctrlr.GetItem(&item)))//always stops here on first try, why? { AfxMessageBox("It didn't work!"); continue; } item.mask = LVIF_STATE; item.state = LVIS_SELECTED; ctrlr.SetItem(&item); // None of the items end up being selected. } } What am I doing wrong here? All help appricated. __________ Kevin Bryan kbryan@geocities.com Independent Software Developer IRC (IceNet) Nick:Million http://www.geocities.com/SiliconValley/Pines/4066/ __________________________
Peter M. Yee -- petery@neosoft.com Sunday, March 30, 1997 Just off hand, should the loop be: for (int cnt =3D ctrlr.GetItemCount() - 1 ; cnt >=3D 0; cnt--)??? Shouldn't you be trying to going from n - 1 to 0? The count is always = one more than the largest index. On the first iteration, you're asking = for an entry that doesn't exist. Hope this helps, Peter M. Yee -----Original Message----- From: Kevin Bryan [SMTP:kbryan@geocities.com] Sent: Friday, March 28, 1997 4:20 PM To: mfc-l@netcom.com Subject: CListView::OnSelectAll Environment: MSVC++ 4.0, Windows 95 I'm trying to select all of the items in my CListView-AppWizard-derived-Window. Here is the code I'm using: void CServerView::OnEditSelectAll()=20 { CListCtrl& ctrlr =3D GetListCtrl(); LV_ITEM item; for (int cnt =3D ctrlr.GetItemCount(); cnt >=3D 0; cnt--) { item.iItem =3D cnt; item.iSubItem =3D 0; item.mask =3D LVIF_STATE; item.stateMask =3D0; if (!(ctrlr.GetItem(&item)))//always stops here on first try, why? { AfxMessageBox("It didn't work!"); continue; } item.mask =3D LVIF_STATE; item.state =3D LVIS_SELECTED; ctrlr.SetItem(&item); // None of the items end up being selected. } } What am I doing wrong here? All help appricated. __________ Kevin Bryan kbryan@geocities.com Independent Software Developer IRC (IceNet) Nick:Million http://www.geocities.com/SiliconValley/Pines/4066/ __________________________
Mike Blaszczak -- mikeblas@nwlink.com Sunday, March 30, 1997 [Mini-digest: 2 responses] At 17:19 3/28/97 -0500, Kevin Bryan wrote: >Environment: MSVC++ 4.0, Windows 95 > >I'm trying to select all of the items in my >CListView-AppWizard-derived-Window. Here is the code I'm using: > >void CServerView::OnEditSelectAll() >{ > CListCtrl& ctrlr = GetListCtrl(); > LV_ITEM item; > for (int cnt = ctrlr.GetItemCount(); cnt >= 0; cnt--) > { > item.iItem = cnt; > item.iSubItem = 0; > item.mask = LVIF_STATE; > item.stateMask =0; > if (!(ctrlr.GetItem(&item)))//always stops here on first try, why? > { > AfxMessageBox("It didn't work!"); > continue; > } > item.mask = LVIF_STATE; > item.state = LVIS_SELECTED; > ctrlr.SetItem(&item); // None of the items end up being selected. > } >} > >What am I doing wrong here? All help appricated. You're selecting one more item than you should be. If a list view control has 37 items in it, GetItemCount() on that control will return 37. The items in the control are numbered 0 through 36. Since you're trying to get an item that doesn't exist, your call fails. Incidentally, you're initializing stateMask incorrectly both times that you use it. You could code your loop like this, then: void CServerView::OnEditSelectAll() { CListCtrl& ctrlr = GetListCtrl(); LV_ITEM item; // MIKEBLAS changed the next line for (int cnt = ctrlr.GetItemCount()-1; cnt >= 0; cnt--) { item.iItem = cnt; item.iSubItem = 0; item.mask = LVIF_STATE; item.stateMask = LVIS_SELECTED; // added by MIKEBLAS if (!(ctrlr.GetItem(&item))) { AfxMessageBox("It didn't work!"); continue; } item.mask = LVIF_STATE; item.stateMask = LVIS_SELECTED; // added by MIKEBLAS item.state = LVIS_SELECTED; ctrlr.SetItem(&item); // None of the items end up being selected. } } and that'll work. But that's still woefully inefficient because there's really no reason to call GetItem() in the first place. You could code: void CServerView::OnEditSelectAll() { CListCtrl& ctrlr = GetListCtrl(); LV_ITEM item; item.iSubItem = 0; item.mask = LVIF_STATE; item.stateMask = LVIS_SELECTED; item.state = LVIS_SELECTED; for (int cnt = ctrlr.GetItemCount()-1; cnt >= 0; cnt--) { item.iItem = cnt; if (!ctrlr.SetItem(&item)) TRACE1("It didn't work for item %d\n", cnt); } } and enjoy managing less code and having better performance. .B ekiM Crotch Rocket / Full-Body Rocket / Trip Report Central! 95 Honda VFR-750F / 94 Mazda RX-7 / http://www.nwlink.com/~mikeblas/ Less work, more hockey! These words are my own - I do not speak on behalf of Microsoft. -----From: SyedAt 05:19 PM 3/28/97 -0500, you wrote: >Environment: MSVC++ 4.0, Windows 95 > >I'm trying to select all of the items in my >CListView-AppWizard-derived-Window. Here is the code I'm using: > >void CServerView::OnEditSelectAll() >{ Use this code:- SetFocus(); // so that items are shown selected when it's not in focus.. int n = GetListCtrl().GetItemCount(); for (int i= 0 ; i < n; ++i) GetListCtrl().SetItemState(i, LVIS_SELECTED, LVIS_SELECTED); I think you fail to get your code working because of bad documentation (both WINAPI and MFC). Neither of them mention that we have to use LVIS_SELECTED on both (why.? it looks plain weird)
Mike Blaszczak -- mikeblas@nwlink.com Monday, March 31, 1997 >-----From: Syed>Use this code:- > SetFocus(); // so that items are shown selected when it's not in focus.. What's this SetFocus() call for? Your comment doesn't make much sense to me. > int n = GetListCtrl().GetItemCount(); > for (int i= 0 ; i < n; ++i) > GetListCtrl().SetItemState(i, LVIS_SELECTED, LVIS_SELECTED); > >I think you fail to get your code working because of bad documentation (both >WINAPI and MFC). You should write to vcdocs@microsoft.com and express that opinion. >Neither of them mention that we have to use LVIS_SELECTED >on both (why.? it looks plain weird) One parameter specifies a mask of bits that you're interested in setting or clearning, and the other parameter specifies the new values for the in that mask. .B ekiM http://www.nwlink.com/~mikeblas/ These words are my own. I do not speak on behalf of Microsoft. One is too many and a million is not enough.
Alex Fok -- alexp@sharnoa.co.il Monday, March 31, 1997 Kevin Bryan wrote: > > Environment: MSVC++ 4.0, Windows 95 > > I'm trying to select all of the items in my > CListView-AppWizard-derived-Window. Here is the code I'm using: > > void CServerView::OnEditSelectAll() > { > CListCtrl& ctrlr = GetListCtrl(); > LV_ITEM item; > for (int cnt = ctrlr.GetItemCount(); cnt >= 0; cnt--) > { > item.iItem = cnt; > item.iSubItem = 0; > item.mask = LVIF_STATE; > item.stateMask =0; > if (!(ctrlr.GetItem(&item)))//always stops here on first try, why? > { > AfxMessageBox("It didn't work!"); > continue; > } > item.mask = LVIF_STATE; > item.state = LVIS_SELECTED; > ctrlr.SetItem(&item); // None of the items end up being selected. > } > } > > What am I doing wrong here? All help appricated. > __________ > Kevin Bryan > kbryan@geocities.com > Independent Software Developer > IRC (IceNet) Nick:Million > http://www.geocities.com/SiliconValley/Pines/4066/ > __________________________ Hi! You have at least one little bug in Your code! You are trying to access item number N at first time, but if You have N items, there are 0..(N-1) items available, and no item N exists! I thought, this list is supposed for advanced MFC programmers. This question may be answered in any C language course for beginners. -- ____________________________________________________________ | | | | Alexander Fok | E-mail: alexp@sharnoa.co.il | | Software Engineer | Office: +972-7-6285058 | | | Fax: +972-7-6277859 | | Xtrol (Sharnoa) | Home: +972-7-6271886 | | Corporation Israel | WWW : http://www.cs.bgu.ac.il/~alexf | |_____________________|______________________________________|
Syed -- sxs296@psu.edu Saturday, April 05, 1997 At 09:31 AM 3/31/97 -0800, you wrote: > >>-----From: Syed> >>Use this code:- >> SetFocus(); // so that items are shown selected when it's not in >focus.. > >What's this SetFocus() call for? Your comment doesn't make much sense to me. I have never thought that anyone would pay attention to my comment - I just included it because I felt like want to :). Anyway, what I meant was whenever the ListCtrl has no style LVS_SHOWSELALWAYS, the items selected will not show the selection focus if it's not currently in focus. I read in the documentation:- LVS_SHOWSELALWAYS Always show the selection, if any, even if the control does not have the focus. Correct me if I'm wrong (please). >>Neither of them mention that we have to use LVIS_SELECTED >>on both (why.? it looks plain weird) > >One parameter specifies a mask of bits that you're interested in setting or >clearning, and the other parameter specifies the new values for the >in that mask. > It's just that from my logical point of view, the 'correct' way should be like this:- GetListCtrl().SetItemState(i, LVIS_SELECTED, 1) // Select or GetListCtrl().SetItemState(i, LVIS_SELECTED, 0) // Deselect The code above make perfect sense even to those who are novice in windows programming.
Become an MFC-L member | Вернуться в корень Архива |