1. Home
  2. Computing & Technology
  3. Delphi Programming

How to right align items in a TComboBox

By Zarko Gajic, About.com

Suppose you have a ComboBox displaying currency values, or more generally numbers. Here's how to display the items in a ComboBox right-aligned:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.FormCreate(Sender: TObject) ;
begin
   ComboBox1.Style := csOwnerDrawFixed;


//for a ListBox use "lbOwnerDrawFixed"
end;

//The OnDrawItem event handler
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
   Rect: TRect; State: TOwnerDrawState) ;
var
   x: Integer;
   txt: String;
begin
   with ComboBox1 do
   begin
     Canvas.FillRect(Rect) ;
     txt := Items[Index];
     x := Rect.Right - Canvas.TextWidth(txt) - 4;
     Canvas.TextOut(x, Rect.Top, txt) ;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Here are some more owner drawing articles:
Graphical Combos

Delphi tips navigator:
» Positioning Web controls in design view (Asp.Net - Delphi for .Net)
« How to print a document/page in a TWebBrowser

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TComboBox
  6. How to right align items in a TComboBox

©2009 About.com, a part of The New York Times Company.

All rights reserved.