Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combo Box Font Editing Delphi

Posted on 2012-04-03
4
Medium Priority
?
2,015 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 28

Assisted Solution

by:Sinisa Vuk
Sinisa Vuk earned 668 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 668 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 23

Accepted Solution

by:
Ferruccio Accalai earned 664 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Six Sigma Control Plans
Suggested Courses

604 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