Solved

Multiple Fonts

Posted on 2004-04-07
11
482 Views
Last Modified: 2013-12-03
I am using a combobox and I would like to know if there is a way to have two different fonts - one for the items and one for the original text.

Also the number of items is unknown - it could 2 or it could be 10.

Any help greatly appreciated.

Siwiher :-)
0
Comment
Question by:siwiher
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10779040
Quick & dirty!

Yes, you could set the font at design time for the text property.

Then at runtime, in the onDropDown event of the comboBox, set the font to something else

then in the OnCloseUp return

procedure TForm1.ComboBox1DropDown(Sender: TObject);
begin
 ComboBox1.Font.Name:= 'Arial';
end;

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
 ComboBox1.Font.Name:= 'MS Serif';
end;


Shane

 
0
 
LVL 17

Expert Comment

by:mokule
ID: 10779065
or set

ComboBox1.Style := csOwnerDrawFixed;
or
ComboBox1.Style := csOwnerDrawVariable;

and draw content by yourself

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
//
end;

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
begin
//
end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10779208
Yup, and if ComboBox1.Style := csOwnerDrawFixed then
 heres some example code

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
 with ComboBox1.Canvas do
 begin
  FillRect(Rect);
  Font.Color := clRed;
  Font.Name:= 'Arial';
  TextOut(Rect.Left, Rect.Top, ComboBox1.Items[Index]);
 end;
end;
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10779255
This is written in c++ builder but I can convert to delphi when I get to my home machine.
Set ComboBox1->Stley = csOwnerDraw.

In particular you'll want to look at the InitComobBox method.  When adding strings to your combo box use the AddObject method To Associate a string and a particular Font of interest.  By doing things this way Each item in the combo box can have its own uniqe font.  You just have to be carefull to free the fonts when you remove an item or clear the combo box.

As I said I can convert this to Delphi when I get home if need be but you should be able to look at the code and just get rid of the * and conver the -> to . (a dot).  If you need help conveting please ask and will do so when I get home.  Machine at work doesn't have Delphi :)


void __fastcall TForm1::FormCreate(TObject *Sender)
{
  InitComboBox();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  ClearComboBox();
}
//---------------------------------------------------------------------------
void  __fastcall TForm1::InitComboBox()
{
  TFont* font = new TFont();
  font->Assign(ComboBox1->Font);
  ComboBox1->Items->AddObject("normal font", font);

  font = new TFont();
  font->Assign(ComboBox1->Font);
  font->Style = TFontStyles() << fsBold;
  ComboBox1->Items->AddObject("bolded font", font);

  font = new TFont();
  font->Assign(ComboBox1->Font);
  font->Style = TFontStyles() << fsItalic;
  ComboBox1->Items->AddObject("Italic font", font);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearComboBox()
{
  int numObjects = ComboBox1->Items->Count;
  for(int i = 0; i < numObjects; i++)
  {
    delete ComboBox1->Items->Objects[i];
  }
  ComboBox1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
      TRect &Rect, TOwnerDrawState State)
{
  //Cast the TObject to a TFont
  TFont* font = (TFont*)(ComboBox1->Items->Objects[Index]);

  //Assign the font that we stored
  ComboBox1->Canvas->Font->Assign(font);

  //Print out string out to the canvas
  ComboBox1->Canvas->TextOut(Rect.Left, Rect.Top, ComboBox1->Items->Strings[Index]);
}
//---------------------------------------------------------------------------
0
 

Author Comment

by:siwiher
ID: 10779280
Thanks for the comments lads

I'm trying your ideas now
0
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.

 

Author Comment

by:siwiher
ID: 10779352
>> PerryDk after attempting conversion please could i have some help?

>> To all

the situation is -  the combobox1.text = 'Select Word'  when the form shows

when the user dropsdown the box I want the' Select Word' part to stay normal but the items to be displayed, in this case, Ancient Greek (don't worry i have the font already)

I need the combobox style to remain csdropdown.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10779376
You wont be able to do this with style csDropDown, unless you use my first method above

the only other way, is with

Style  = csOwnerDrawFixed OR  csOwnerDrawVariable

Shane
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10779434
You can't have the combobos style to remain as csDropDown and have your items being displayed in more than one font.  In order to have the some of the items in normal font and some of the items in another font you have to use the OnDrawItem event.  This event requires That the TComboBox have a Style of csOwnerDrawFixed or csOwnerDrawVariable to work properly.

if you want just the selected one to be in one font and others do this in your OnDrawItem event handler (I'm a little rusty on my Delphi so you'll have to forgive mistakes until I get home where I can verfiy my syntax is correct.


  if Index = ComboBox1.ItemIndex then
  begin
     //this item is selected so use the font for a selected item
     //use the TextOut as described earlier
  end;
  else begin
     //this item is not selected so use  the appropriate font for that
     //Use the TextOut as described earlier
  end;


0
 
LVL 17

Accepted Solution

by:
mokule earned 200 total points
ID: 10779475
Do it like this.

Place Edit on a ComboBox such that it covers editing area of ComboBox.
Set Fonts separately to Combo and to Edit.
I'm not sure what You want after selecting word.

add procedure

procedure TForm1.Edit1Click(Sender: TObject);
begin
  ComboBox1.DroppedDown := True;
end;

You can do whatever You like.
0
 

Author Comment

by:siwiher
ID: 10781615
thanks mokule it worked perfectly!
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10782676
Yeah, it worked so perfectly, he gave you a "B"

<Smile>

Shane
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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

911 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

20 Experts available now in Live!

Get 1:1 Help Now