Recent

Author Topic: Strange behaviors with TListView  (Read 590 times)

egsuh

  • Full Member
  • ***
  • Posts: 241
Strange behaviors with TListView
« on: September 19, 2019, 07:40:02 am »
Event handlers of TListView is not working as expectd.

I wrote OnEnter and OnSelectItme  event handlers (as simple as showmessage). What I expect is that when the focus moves from some other control on the form to the ListView then OnEnter must be called first, and then OnSelectItem must follow. Delphi does exactly that. But this does not happen consistently in Lazarus. 

zeljko

  • Hero Member
  • *****
  • Posts: 1079
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Strange behaviors with TListView
« Reply #1 on: September 19, 2019, 09:28:10 am »
Where ? OS ? Widgetset ? Lazarus version , fpc version ?

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #2 on: September 19, 2019, 09:34:21 am »
Windows 7 Home Premium K Service Pack 1
Lazarus verison 2.0.4
FPC 3.0.2

Widgetset?  Not sure, probably no. (Frankly speaking I don't know what widgetsets are). 

Thaddy

  • Hero Member
  • *****
  • Posts: 9160
Re: Strange behaviors with TListView
« Reply #3 on: September 19, 2019, 10:06:03 am »
A widget set is nothing more than the OS (or framework) supplied what you would call "controls". In other words all the basic programming is already done for you.
Lazarus (or Delphi) simply provide a very high level ( easier to use) abstraction.

For you - since you are very curious - it would be a good idea to investigate things, instead of asking them: you can be a very good programmer!!! You are on the right path!
« Last Edit: September 19, 2019, 10:08:14 am by Thaddy »
also related to equus asinus.

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #4 on: September 19, 2019, 10:32:30 am »
@Thaddy

I really appreciate your comments and advices (and other people's as well). Well, I ask questions frequently here and sorry if I caused you any inconveniences. But for now I'm not asking on widgetset.  I just reported on my system.

wp

  • Hero Member
  • *****
  • Posts: 6312
Re: Strange behaviors with TListView
« Reply #5 on: September 19, 2019, 10:57:51 am »
Event handlers of TListView is not working as expectd.

I wrote OnEnter and OnSelectItme  event handlers (as simple as showmessage). What I expect is that when the focus moves from some other control on the form to the ListView then OnEnter must be called first, and then OnSelectItem must follow. Delphi does exactly that. But this does not happen consistently in Lazarus.
I tested this - yes, you are right: When the ListView is not focused and I click on a ListItem I see the OnSelectItem event firing first with parameter Selected=false, then a second time with parameter Selected=true, and finally OnEnter. In Delphi 7 and XE 10.3, the OnEnter event fires before the OnSelectItem events.

But the selection can be changed by code even when the ListView is not focused. Therefore it is not absolutely required by logics that OnEnter comes before OnSelectItem. I don't know why this was implemented in Lazarus in the opposite order from Delphi, maybe to simplify operation of other widgetsets. Changing this, I fear, will  break numerous Lazarus application relying on the current sequence of events - therefore, I guess this will not happen.

In general I think you should not rely on consistent order of events when changing between Delphi and Lazarus, and even within Lazarus when changing between different widgetsets.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #6 on: September 19, 2019, 11:34:33 am »
I STRONGLY DISAGREE with you on this issue.

The wrong order is peculiar to TListView.  It follows the order of

1) OnSelectItem of unselected item
2) OnSelectItem of selected item
3) OnExit of previously focused control (if Listview itself was not focused)
4) OnEnter of ListView itself.

Think of a situation. I did some editing on Edit1.  And I want to save the content of Edit1 to another variable within Edit1Exit procedure, and fill in the Edit1 with new value (for example, ListView1.SubItems[2].Text) at the moment of clicking any listview item. If thing happen in the above order, the Edit1 content will be filled with new value (by ListView1SelectItem) before saved to a variable (by Edit1Exit procedure). 

Treeview is different. Here,

1) Other control's  OnExit
2) Treeview OnEnter
3) Treeitem OnChanging
4)  Treeview OnSelectionChange
5) Treeitem OnChange

This is right and normally expected order.

Still there is a bug. In Treeview's OnChanging event, this should happen to a treenode which is about to be "UNSELECTED".  Currently newly SELECTED node is passed as a parameter both in OnChanging and OnChange methods. 

I had to rewrite some procedures when I transformed Delphi program to Lazarus. In Delphi, I can finish works on previous node at OnChanging, and then fill the controls on the form with values of newly selected node (at OnChange event). This is not possible in Lazarus.


wp

  • Hero Member
  • *****
  • Posts: 6312
Re: Strange behaviors with TListView
« Reply #7 on: September 19, 2019, 12:11:48 pm »
Think of a situation. I did some editing on Edit1.  And I want to save the content of Edit1 to another variable within Edit1Exit procedure, and fill in the Edit1 with new value (for example, ListView1.SubItems[2].Text) at the moment of clicking any listview item. If thing happen in the above order, the Edit1 content will be filled with new value (by ListView1SelectItem) before saved to a variable (by Edit1Exit procedure). 
A nice example. But I could imagine that there are examples for the opposite scenario in this kind of race condition...

I would select other events for your purpose. Since at any time at most one control can be focused the OnExit event of Edit1 must occur before OnEnter of TListView. This is the logical order of these events. There is no logical order for TEdit.OnExit and TListView.OnSelectItem because TListView.OnSelectItem can occur at any time. Therefore I'd choose TListView.OnEnter instead of TListView.OnSelectItem.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #8 on: September 19, 2019, 12:18:33 pm »
TListView.OnEnter does not fire when the selection is changed within TListView, not leaving the TListView.

wp

  • Hero Member
  • *****
  • Posts: 6312
Re: Strange behaviors with TListView
« Reply #9 on: September 19, 2019, 12:23:16 pm »
Then put the code in both, but exit OnSelectItem when ListView is not focused. There are probably more ways to make it work without relying on an undefined order of unrelated events.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #10 on: September 19, 2019, 12:38:00 pm »
There are more inconsistences in TListView's events.

In my second example, please follow steps.

1.  press 'Clear Memo' button
2. Enter Edit1
3. Select List Item 1 or List Item 2

list item 1 or two OnSelection does not occur. There are only two events. Edit1Exit and ListView1Enter.

I tried TListView.OnChange, but onchange events occur twice for one list item.

I cannot find any reliable way based on TListView's events.

wp

  • Hero Member
  • *****
  • Posts: 6312
Re: Strange behaviors with TListView
« Reply #11 on: September 19, 2019, 01:20:57 pm »
"second example"? I see only one, the "event order.zip" of reply #6 with a single project.

I cannot reproduce the described behavior with this example: The steps "Press 'Clear memo'", "focus Edit1", "select one of the two items of the Listview" result in the following output which looks reasonable to me
Quote
List Item 1 selected
Edit1 Exit
Listview OnEnter
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #12 on: September 19, 2019, 01:30:50 pm »
Quote
"second example"? I see only one, the "event order.zip" of reply #6 with a single project.

I mean it. Sorry. I confused with example I attached at another thread.

Quote
I cannot reproduce the described behavior with this example:

This is not consistent. In my PC, sometimes 'list item selected' is done, in other cases not.

wp

  • Hero Member
  • *****
  • Posts: 6312
Re: Strange behaviors with TListView
« Reply #13 on: September 19, 2019, 04:41:35 pm »
Quote
I cannot reproduce the described behavior with this example:
This is not consistent. In my PC, sometimes 'list item selected' is done, in other cases not.
Do this in your demo:
(1) Select "List Item 1" in the ListView
(2) Clear the memo
(3) Click into Edit1
(4) Click "List Item 1" again
--> In going from step (3) to (4) the memo records only "Edit1 Exit and "ListView OnEnter", no "List Item 1 selected". But this is normal because Item1 had already been selected in step (1). So, consistent behavior.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

egsuh

  • Full Member
  • ***
  • Posts: 241
Re: Strange behaviors with TListView
« Reply #14 on: September 20, 2019, 05:57:53 am »
I see. You are right.

Even though ListView itself is not focused, list item might be in the state of "selected", and OnSelectItem event does not fire when already-selected item is selected again.

But developers must be very careful if they want to write application based on events.