Skip to main content

Example: Overriding LoadFontNames procedure

  • 3 minutes to read

This example demonstrates overriding the default behavior of the cxFontNameComboBox control. By default, it loads the names of all fonts available in the system. You can implement custom font loading by overriding the LoadFontNames procedure. The functionality of the cxFontNameComboBox is overridden in this example to so that fonts are loaded from a text file.

Two classes are created – TMyFontNameComboBox and TMyFontNameComboBoxProperties. The TMyFontNameComboBoxProperties declares a FileName property, specifying the name of the file, containing the font names to load. The properties class also contains an overridden LoadFontNames procedure.

If the file specified via the FileName property exists, then the font names from this file are loaded into the font name combo box. Changing of the FileName property forces a call of the LoadFontNames method. This behavior is implemented in the TMyFontNameComboBoxProperties.SetFileName private method.

The TMyFontNameComboBox class overrides the GetPropertiesClass class function in order to specify the required properties class (TMyFontNameComboBoxProperties).

type
//The TMyFontNameComboBoxProperties class contains the FileName property
//declaration and the overriden LoadFontNames procedure
  TMyFontNameComboBoxProperties = class(TcxFontNameComboBoxProperties)
  private
    FFileName: string;
    procedure SetFileName(Value: string);
  public
    property FileName: string read FFileName write SetFileName;
    procedure LoadFontNames; override;
  end;
//The TMyFontNameComboBox class contains the overridden GetPropertiesClass class function
  TMyFontNameComboBox = class(TcxFontNameComboBox)
  public
    class function GetPropertiesClass: TcxCustomEditPropertiesClass; override;
  end;
implementation
{ TMyFontNameComboBoxProperties }
//The following method loads the names of the fonts from a text file
procedure TMyFontNameComboBoxProperties.LoadFontNames;
begin
//removing existing items from the font name combo box
  Self.Items.Clear;
//if the FileName property specifies the name of an existing file, then this
//file's contents are loaded into the Items property of the font name combo box
  if FileExists(FFileName) then
    Self.Items.LoadFromFile(FFileName)
  else Exit;
end;
//The following method changes the FileName property value
procedure TMyFontNameComboBoxProperties.SetFileName(Value: string);
begin
  if FFileName <> Value then
  begin
//the new value is assigned to the FileName property
    FFileName := Value;
//and the LoadFontNames method is called to reflect the changing of the font
//names source
    LoadFontNames;
  end;
end;
{ TMyFontNameComboBox }
//The following function returns the class of the properties object associated
//with the editor
class function TMyFontNameComboBox.GetPropertiesClass: TcxCustomEditPropertiesClass;
begin
//The Properties property of the TMyFontNameComboBox object is interpreted as an
//instance of the TMyFontNameComboBoxProperties class
  Result := TMyFontNameComboBoxProperties;
end;
//...
//...
var
  Form1: TForm1;
//MyFontNameCombo object represents the TMyFontNameComboBox object
  MyFontNameCombo: TMyFontNameComboBox;
//...
procedure TForm1.FormCreate(Sender: TObject);
begin
//creating TMyFontNameComboBox control
  MyFontNameCombo := TMyFontNameComboBox.Create(Self);
  MyFontNameCombo.Parent := Self;
//setting the FileName property of the created control. Fonts in the specified
//file are automatically loaded into the font name combo box
  TMyFontNameComboBoxProperties(MyFontNameCombo.Properties).FileName := 'c:\FontNames.ini';
end;
//...

The FontNames.txt file contains four font names: Arial, Courier, Times New Roman and MS Sans Serif.

The following screenshot demonstrates the generated MyFontNameComboBox object: