Recent

Author Topic: TListView.FindCaption seems to count from -1  (Read 3831 times)

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
TListView.FindCaption seems to count from -1
« on: February 10, 2016, 11:00:04 pm »
I'm using FindCaption" to get items in a TListView (This is just a small list, 10 lines or so)

To find the first Item, the StartIndex have to be set to -1.
I had expected the first Item as 0.

ListView1.Selected:=ListView1.FindCaption(-1,'Test0',False,False,False,True);  <--- This works to find the first item
ListView1.Selected:=ListView1.FindCaption(0,'Test0',False,False,False,True);  <--- This does not.

The second item is found if StartIndex=0, but not 1.
The third item     is found if StartIndex=1, but not 2.

Is this a bug, a feature or ... ?
« Last Edit: February 10, 2016, 11:04:09 pm by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4460
  • I like bugs.
Re: TListView.FindCaption seems to count from -1
« Reply #1 on: February 10, 2016, 11:46:47 pm »
I'm using FindCaption" to get items in a TListView (This is just a small list, 10 lines or so)

To find the first Item, the StartIndex have to be set to -1.
I had expected the first Item as 0.

ListView1.Selected:=ListView1.FindCaption(-1,'Test0',False,False,False,True);  <--- This works to find the first item
ListView1.Selected:=ListView1.FindCaption(0,'Test0',False,False,False,True);  <--- This does not.
...
Is this a bug, a feature or ... ?

Appears to be a feature. See, the fourth parameter is "Inclusive". You should make it True.
The whole parameter feels useless and strange. I believe it is there for Delphi compatibility.

BTW, you could have figured this out by yourself by Ctrl-clicking the method name, and then Ctrl-clicking again to get to TListItems.FindCaption and studying the code.
I did that and ... good news: I also found a bug which I would not have found otherwise. Fixed in r51588.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TListView.FindCaption seems to count from -1
« Reply #2 on: February 10, 2016, 11:48:02 pm »
The indexes refer to those of the SubItems of the TListItem. Since in report view there is also the Caption the indexes appear to be offset by 1.

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
Re: TListView.FindCaption seems to count from -1
« Reply #3 on: February 11, 2016, 08:06:19 am »
Ok, Thanks guys.

By the way,
I'm using the ListView1.FindCaption to set focus on a selected item,
This is ok when a item is selected by a mouse click,

But at the program start, the first item has allways to be selected/Set focus to.
Have tried this from FormCreate, FormActive and FormPaint, all are giving error "Can not SetFocus".

To solve this problem I'm using a timer, start at FormCreate, and on timer interval 1, set focus is ok.
Question, could this been done in a better way?
 

« Last Edit: February 11, 2016, 08:56:09 am by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TListView.FindCaption seems to count from -1
« Reply #4 on: February 11, 2016, 09:24:32 am »
Don't understand what you're doing. You want to have the Listview focused and fail with Listview.SetFocus? Use Form.ActiveControl := Listview instead. Use ListView.ItemIndex := 0 to select the 1st list item (i.d. index 0). Set HideSelection to false to see the selected item highlighted even if the listview is not focused.

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
Re: TListView.FindCaption seems to count from -1
« Reply #5 on: February 11, 2016, 10:06:10 am »
Thanks;
Form.ActiveControl := Listview;
ListView.ItemIndex := 0;
This did the trick!
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

 

TinyPortal © 2005-2018