Change the selection opacity of a ListBox

  

I have trouble changing the selection opacity of the FireMonkey ListBox. In my case I wanted to change the color of the selection, and make it fully opaque. At first I tried to do this by changing the style of de ListBox through a custom style based on the default. The selection part is a TStyleObject with an opacity of 1. I replaced it with a TRectangle filled with the color Red. After I applied my style I noticed that the selection was not the same bright red color that I chose for the selection.

So I just dove into the FMX.ListBox code to see what is happening there. To my surprise I found that the opacity of the selection is set to 0.7 hard-coded in the TCustomListBox ApplyStyle procedure. I expected that the style was responsible for how things looked. I don’t know if it’s the wrong way to think about it, but this is not what I expected.

After this I tried changing the selection opacity run-time by implementing the OnApplyStyleLookup event with the following code.

procedure TForm1.ListBox1ApplyStyleLookup(Sender: TObject);
var
SelectionControl: TControl;
begin
if ListBox1.FindStyleResource<TControl>(‘selection’, SelectionControl) then
SelectionControl.Opacity := 1;
end;

This however has no effect. I tried to do the same thing in a new custom ListBox control by deriving from TCustomListBox and overriding the ApplyStyle method where after the ApplyStyle of TCustomListBox through the inherited keyword I placed the code to set the selection opacity to 1. This too did not have any effect.

Am I just going about this the wrong way, am I missing something, or is it just Delphi being weird.

Comments are closed.