Color Picker on existing applications DB

11 views
Skip to first unread message

Ozzie Levesque

unread,
Oct 25, 2024, 11:52:07 AMOct 25
to Rosi Delphi Components
I have an application that saves colors in integer format.

When the column is set as ColorPicker the TrDBGrid shows the colors correctly as colors although it shows INTEGERS next to it. 

But when I change the color with the picker it tries to save it to the DB in String format which, causes an error as the DB on accepts an integet. 

As easy as it is to switch the DB field to a String format, it breaks ALL of the rest of the application. Is there anyway to rewrite what the Grid field displays and saves BEFORE it is shown or saved?

Thanks,
Ozzie

Tomas Rosinsky

unread,
Oct 29, 2024, 5:05:59 PMOct 29
to Rosi Delphi Components
Hello,

you are right, by default color is stored as ColorName (Red, Blue..) or as RGB value ($00C86CD2) by rDBGrid,
But you can use Field events GetText and SetText to convert string values to integer value by one line of code:


uses rpictool;

procedure TDMData.MyFieldGetText(Sender: TField; var Text: string;
  DisplayText: Boolean);
begin
  Text:=ColorToString(Sender.AsInteger);
end;

procedure TDMData. MyFieldSetText(Sender: TField; const Text: string);
begin
  Sender.AsInteger:=StringToColor(Text);
end;



Then it will work also with integer field.

There is also small issue with selection of custom color with integer field in the current version which can be easily fixed and will be available in the next version. 
In meantime you can update rDBGrid source manually as follows, just replace whole function:


function TrDBGridInplaceEditEx.CheckSelectUserDefinedColor(F: TField): boolean;
var
  Dlg: TColorDialog;
  userDefColor: boolean;
begin
  Result:=false;
  if F is TNumericField then
    userDefColor:=rpictool.GetColorIdx(F.AsInteger) = ColorDefCustomIdx
  else
    userDefColor:=rpictool.GetColorIdx(F.AsString) = ColorDefCustomIdx;
  if userDefColor then
  begin
    Dlg:=TColorDialog.Create(self);
    Dlg.Color:=rpictool.StringToColor(oldFieldColorString);
    Dlg.Options:=[cdFullOpen, cdAnyColor];
    try
      if Dlg.Execute(Handle) then
      begin
        if F is TNumericField then
          F.AsInteger:=Dlg.Color
        else
          F.AsString:='$' + IntToHex(ColorToRGB(Dlg.Color), 8);
        Result:=true;
      end
      else
        F.AsString:=oldFieldColorString;
    finally
      FreeAndNil(Dlg);
    end;
  end;
end;


I hope it helps.
Tomas

Reply all
Reply to author
Forward
0 new messages