delphibackground-colorcolordialog

Delphi - Colordialog select Black as color


I'm new in Delphi and I need some help.

I have program, a background color changer, with a TColorDialog. This works otherwise perfectly, but there is a problem with it when I close the ColorDialog without choosing a color from it. Then the background is changing in to black and it also save black as color "0" in the .ini file.

my code:

procedure TEinstellungenF.BtnBackgroundClick(Sender: TObject);
var filename:String;
ini:TIniFile;
begin       // Speichert die im ColorDialog ausgewählten Background Farben in der Ini
  filename := ExtractFilePath(ParamStr(0)) + 'Einstellungen.ini';
  ini := TIniFile.Create(filename);
  ColorDialog1.Execute();
  try
    ini.WriteInteger('Farben','Hintergrundfarbe', ColorDialog1.Color);
  finally
    ini.Free;
  end;

  FrmMain.Color := TColor(Ini.ReadInteger('Farben','Hintergrundfarbe',ColorDialog1.Color));
  Color := TColor(Ini.ReadInteger('Farben','Hintergrundfarbe',ColorDialog1.Color));
end;``` 

Solution

  • Check the returned value of ColorDialog1.Execute and act accordingly.

    From the docs:

    Execute opens the color-selection dialog, returning true when the user selects a color and clicks OK, or false when the user cancels.