Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2019-08

TListView - how to make selected line stay selected?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

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 columns.
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);
var
  i, l: integer;
  V1: TVideoCut;
begin
  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
end;

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 still selected.
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)...

Bo Berglund Developer in Sweden

Reply by email

Bo Berglund
to Bo Berglund
Bo Berglund wrote
How can this be accomplished?

I ended up adding the code below after reading this: http://delphidabbler.com/articles?article=16

procedure TfrmEditCuts.lvCutListCustomDrawItem(Sender:
TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
const
  cStripe = $FF9933;  // colour of selected list items
begin
  if Item.Index = lvCutList.ItemIndex then
  begin
    lvCutList.Canvas.Brush.Color := cStripe;
    lvCutList.Canvas.Font.Color := clWhite;
  end
  else
    lvCutList.Canvas.Brush.Color := clWindow;
end;

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....

Bo Berglund Developer in Sweden

Reply by email

Bo Berglund
to Werner Pamler
Werner Pamler 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 work.
When I returned to the listview the selected item was unselected immediately...
So I have to keep my changes.

Bo Berglund Developer in Sweden

Reply by email