How to change highlight appearance of ListBoxItems

260 Views Asked by At

Whenever a ListboxItem is selected, I want the appearance of the selected listbox item to change ( stand out). Below is my code that I have. It works properly and as I'd expect, EXCEPT when I scroll down the listbox, and then try to select another ListBoxItem. Then, some items don't change in appearance at all, some don't de-highlight, or the app crashes, etc. Why??

Delphi XE5 for a Mobile iOS application.

procedure TForm5.ListBoxItem1Click(Sender: TObject);
var
  Item : TListBoxItem;
  C : TColorBox;
  T : TText;
  i : Integer;
begin
  i := 0;
  while i <> ListBox1.Items.Count do begin
    Item := ListBox1.ListItems[i];
    T := Item.Children[1] as TText;
    C := Item.Children[2] as TcolorBox;
    T.Color := TAlphaColors.Black;
    C.Visible := False;

    i := i +1;
  end;
  Item := Sender as TListBoxItem;
  T := Item.Children[1] as TText;
  C := Item.Children[2] as TcolorBox;

  T.Color := TAlphaColors.White;
  C.Visible := True;
end;
1

There are 1 best solutions below

0
Aaron Bratcher On BEST ANSWER

Keep track of your selected cell. When the user selects another, make sure you redraw the previously selected cell, then draw your newly selected cell.

pseudo code:

class property: currentCell

onCellSelection:
previousCell = currentCell
currentCell = selectedCell
redrawCells:[previousCell,currentCell]

Your cell drawing code should take into consideration the currentCell property on whether it should draw as hilighted or not.