Skip to main content

TcxCustomColorComboBoxProperties.OnSelectCustomColor Event

Enables you to handle the ability to add custom colors to the color list.

Declaration

property OnSelectCustomColor: TcxSelectCustomColorEvent read; write;

Remarks

You can allow end-users to add custom colors to the list by setting the AllowSelectColor property to True. In this instance, the editor displays an ellipsis button to invoke the standard, advanced or custom color editor dialog. By default, when the user clicks the OK button within this dialog, the chosen color is added to the list and is selected. You can, however, override this default behavior by handling the OnSelectCustomColor event.

The AColor parameter is initially set to the chosen color. You can change it by specifying a custom color.

The AColorDescription parameter allows you to specify the description string for the color.

If you want to prohibit adding the color to the list, set the AddToList parameter to False. This parameter’s default value is True, and thus you don’t need to modify it if the color is to be accepted. Note that if the AddToList parameter is set to False, none of the combo box items are selected. See the DefaultColorStyle property description for details when no items are selected.

The color combo box enables you to provide a custom color editor dialog to end-users. To do so, you need to set the ColorDialogType property to cxcdtCustom. In this instance, clicking the ellipsis button displayed by the editor only raises the OnSelectCustomColor event. Thus, you need to provide your own custom color editor dialog for end-users within the event handler. Please refer to the Creating Custom Color Editing Dialogs topic for details on doing so.

The following sample describes a way in which to handle the OnSelectCustomColor event. The handler allows shades of only gray to be selected. Descriptions are assigned to the new colors in the following format: ‘Shade of grey <number>’, where number is the intensity of white, 0 for black and 255 for white.

procedure TForm1.cxColorComboBox1PropertiesSelectCustomColor(
  Sender: TObject; var AColor: TColor; var AColorDescription: string;
  var AddToList: Boolean);
var
  R, G, B: Integer;
  RGB: string;
begin
  RGB := IntToHex(AColor, 6);
  R := StrToInt('$' + LeftStr(RGB, 2));
  G := StrToInt('$' + MidStr(RGB, 3, 2));
  B := StrToInt('$' + RightStr(RGB, 2));
  if (R = G) and (G = B) then
    AColorDescription := 'Shade of grey ' + IntToStr(R)
  else
  begin
    AddToList := False;
    MessageDlg('Only shades of grey are allowed', mtError, [mbOk], 0);
  end;
end;

The image below shows the appearance of the color combo box after performing such event handling.

Note

modifying the color description in the OnSelectCustomColor event handler only takes effect if the NamingConvention property value is cxncNone. Also, description modifications can be overridden by handling the OnNamingConvention event.

See Also