[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Multiple Fonts

Posted on 2004-04-07
11
Medium Priority
?
506 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
[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
  • 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
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.

 
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
 

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 600 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

656 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