Combo Box Font Editing Delphi

I have an bluetooth application that I have several commands loaded in 3 combo boxes.  I want to be able to edit, and change the text font types & size for each combo box. For example if the the word "Testing" has focus and is selected in combo box 1, I want to be able to change/modify that font to Times or Arial etc., with a size of 10.  If the the word "Connect" has focus and is selected in combo box 2, I want to be able to change/modify that font to some other font and size for that combo box.  I also need to have a default checkbox that changes the font/size in combo box 2 and 3 to whatever has been selected for combo box 1, so that each will have to be changed individually if this is checked.

Please provide and example of how to accomplish this with D6 or D7.  Also, if you know of a component that quickly allows this functionality I will accept that as well, as long as it does not cost a great deal.  

Thanks in advance.

aj85
aj85Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
On first combobox (1 ) set event OnChange with following:

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  s: String;
begin
  s := ComboBox1.Items.Strings[ComboBox1.ItemIndex];
  if s = 'Testing' then
  begin
    ComboBox1.Font.Name := 'Arial';
    ComboBox1.Font.Size := 10;
  end else if s = ... then
  begin
    ComboBox2.Font.Name := 'Tahoma';
    ComboBox2.Font.Size := 11;
  end;
  ...
end;

Open in new window


... you will find the idea. You can use OnDrawItem to custom draw text in combo list/edit ...

http://www.delphifaq.com/faq/f400.shtml
http://delphi.about.com/od/vclusing/a/drawincombobox.htm
0
jimyXCommented:
Use the ComboBox event OnChange and test the selected item.

The following example shows how to change the Font Name, Size, and Color of ComboBox1 and ComboBox2 when changed if the selected items equal to 'Testing' and 'Connect' respectivly:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if UpperCase((Sender as TComboBox).Text) = 'TESTING' then
    begin
      (Sender as TComboBox).Font.Color := clRed;
      (Sender as TComboBox).Font.Size := 10;
      (Sender as TComboBox).Font.Name := 'Arial';
    end;

  if UpperCase(ComboBox2.Text) = 'CONNECT' then
    begin
      ComboBox2.Font.Color := clBlue;
      ComboBox2.Font.Size := 10;
      ComboBox2.Font.Name := 'Times New Roman';
    end;
end;

procedure TForm1.ComboBox2Change(Sender: TObject);
begin
  if UpperCase((Sender as TComboBox).Text) = 'CONNECT' then
    begin
      (Sender as TComboBox).Font.Color := clBlue;
      (Sender as TComboBox).Font.Size := 10;
      (Sender as TComboBox).Font.Name := 'Times New Roman';
    end;

  if UpperCase(ComboBox1.Text) = 'TESTING'' then
    begin
      ComboBox1.Font.Color := clRed;
      ComboBox1.Font.Size := 10;
      ComboBox1.Font.Name := 'Arial';
    end;
end;

Open in new window


Or you can have the two ComboBoxes have the same OnChange event where you can test the content of each ComboBox and set the Font as required:
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if UpperCase(ComboBox1.Text) = 'TESTING' then
    begin
      ComboBox1.Font.Color := clRed;
      ComboBox1.Font.Size := 10;
      ComboBox1.Font.Name := 'Arial';
    end;

  if UpperCase(ComboBox2.Text) = 'CONNECT' then
    begin
      ComboBox2.Font.Color := clBlue;
      ComboBox2.Font.Size := 10;
      ComboBox2.Font.Name := 'Times New Roman';
    end;
end;

Open in new window

0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
What about creating a your own component?
I've created this one for you with two new properties
SpecialFont: TFont and SpecialItemText: String
With this you can set the SpecialFont, that is the font you want to set for a SpecialItemText directly in design time and any ComboBox can be indipendent
So if the user selects an Item that have the same text as SpecialItemText, then the Font is changed to the SpecialFont value, else it returns to the original Font.
Of course you can change these values any time in runtime as you prefere
Give it a try

unit MyComboBox;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, Graphics;

type
  TMyComboBox = class(TComboBox)
  private
    { Private declarations }
    fSpecialFont: TFont;
    fOriginalFont: TFont;
    fSpecialItemText: String;
    procedure SetSpecialFont(value: TFont);
  protected
    { Protected declarations }
    procedure Change; override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property SpecialItemText: String read fSpecialItemText write fSpecialItemText;
    property SpecialFont: TFont read fSpecialFont write SetSpecialFont;
  end;

procedure Register;

implementation

procedure TMyComboBox.SetSpecialFont(value: TFont);
begin
  if fSpecialFont <> value then
    fSpecialFont.Assign(value);
end;

procedure TMyComboBox.Change;
begin
  inherited Change;
  if Uppercase(self.items[self.ItemIndex]) = Uppercase(fSpecialItemText) then
    self.font.Assign(fSpecialFont)
  else if self.font <> fOriginalFont then
    self.font.Assign(fOriginalFont);
end;

constructor TMyComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fSpecialFont := TFont.Create;
  fOriginalFont := TFont.Create;
  fOriginalFont.Assign(self.font);
end;

destructor TMyComboBox.Destroy;
begin
  fSpecialFont.Free;
  fOriginalFont.Free;
  inherited Destroy;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TMyComboBox]);
end;

end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aj85Author Commented:
Thanks everyone, they all worked well and were excellent, but the component was the simplest solution to use.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.