Solved

Combo Box Font Editing Delphi

Posted on 2012-04-03
4
1,688 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 25

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

13 Experts available now in Live!

Get 1:1 Help Now