Search code examples
listviewdelphifiremonkey

Searchbox Text Color in a Listview


I'm trying to change the text color of a Searchbox in a Listview...

Even looking in the "Searchbox" listview control the color can't be changed...

procedure TfrmList.FormShow(Sender: TObject);
begin
var
  i: integer;
  sb: TSearchBox;
begin
  for i := 0 to ListView1.Controls.Count-1 do
    if ListView1.Controls[I].ClassType = TSearchBox then
    begin
      sb := TSearchBox(ListView1.Controls[i]); 
      Break;
    end;

    sb.FontColor := TAlphaColors.White;
    //or
    sb.TextSettings.FontColor := TAlphaColors.White;
end;

Any ideias? Thanks

Change the color settings in the control of the listview.


Solution

  • Add the line shown:

    procedure TfrmList.FormShow(Sender: TObject);
    begin
    var
      i: integer;
      sb: TSearchBox;
    begin
      for i := 0 to ListView1.Controls.Count-1 do
        if ListView1.Controls[I].ClassType = TSearchBox then
        begin
          sb := TSearchBox(ListView1.Controls[i]); 
          Break;
        end;
    
        sb.FontColor := TAlphaColors.White;
        //or
        sb.TextSettings.FontColor := TAlphaColors.White;
        sb.StyledSettings:=sb.StyledSettings-[TStyledSetting.FontColor]; //<--Add this line.
    end;
    

    This is a common problem that people often get caught on. Any text settings you change like that will be ignored if the StyledSettings property doesn't have the related flag cleared. When you change, say, the font colour from the Object Inspector, the StyledSettings property will be updated automatically. But when you make the change programatically, you also need to change StyledSettings in your code.