Solved

Combo Box Font Editing Delphi

Posted on 2012-04-03
4
1,723 Views
Last Modified: 2012-04-08
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
0
Comment
Question by:aj85
4 Comments
 
LVL 26

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 167 total points
ID: 37804784
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
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 167 total points
ID: 37804800
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
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 166 total points
ID: 37804911
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
 

Author Closing Comment

by:aj85
ID: 37821266
Thanks everyone, they all worked well and were excellent, but the component was the simplest solution to use.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now