[Lazarus] TListView - how to make selected line stay selected?
I have a dialog which is used to enable changing the order of certain
items. The items are displayed in a TListView component in report
mode, each item occupies one line and shows several properties in the
Now I want to be able to select the row (list.Rowselect = true) when
clicking on it. This works OK.
Then I have a button which moves the item up and another button to
move it down the list.
In my code I do the following for moving up (lvCutList is the listview
showing the data, FVideoCuts ia a dynamic array of TVideoCut records):
procedure TfrmEditCuts.btnUpClick(Sender: TObject);
i, l: integer;
i := lvCutList.ItemIndex;
if i < 1 then exit; //Not selected or already at top
V1 := FVideoCuts[i-1];
FVideoCuts[i-1] := FVideoCuts[i];
FVideoCuts[i] := V1;
ShowCuts(FVideoCuts); // <= Repopulate the listview from data
lvCutList.ItemIndex := i-1; //<= This is no longer visible
So the end result is that the line is moved up one position in the
list and I set the itemindex property such that the moved line is
But it does not show on screen...
The selection of the line disappeares immediately when the button is
clicked and only returns if I forcibly click in the listview in some
place that will not move the selection, for example in the unpopulated
area at the bottom.
What I would like to see is that the selection color *stays visible*
even if focus is moved away from the listview component.
How can this be accomplished?
I am pretty sure I could do this when I used Delphi a few years back
at work (now retired)...
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
cStripe = $FF9933; // colour of selected list items
if Item.Index = lvCutList.ItemIndex then
lvCutList.Canvas.Brush.Color := cStripe;
lvCutList.Canvas.Font.Color := clWhite;
lvCutList.Canvas.Brush.Color := clWindow;
This made the selected row stay colored even though the listview lost
focus when I clicked the buttons.
I had to probe the selection color in my paint program to set the
cStripe value to the same because when clicking on an item the
CustomDrawItem event seems not to be called. So on initial click the
row was colored blue and when I used my buttons to move the row it was
colored in the cStripe value that I originally used (light green).
Changing the cStripe value to the color of initial selection made the
row move without visible color change....
Re: [Lazarus] TListView - how to make selected line stay selected?
On Tue, 20 Aug 2019 00:01:38 +0200, Werner Pamler via lazarus
<[hidden email]> wrote:
>Did you set HideSelection to false?
Did not know it existed...
Set to true by default.
But when I unchecked it and tried to skip the code I showed it did not
When I returned to the listview the selected item was unselected
So I have to keep my changes.