Solved

Combo Box Font Editing Delphi

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

776 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