From: Ross Levis <[EMAIL PROTECTED]>
> Here's a Windows dev newbie question. I'm use to working in DOS with a
> fixed size font.
> I'm using a Listbox to list 2 column's of data.
> {I could use a StringGrid but I don't need all the added functionality &
> it adds an extra 50k to the size of the EXE. I like to keep things as
> simple as possible.}
> This is done by setting the TabWidth property & separating the 2 fields
> with a tab (^I). It works well except when the first field exceeds the
> length of the TabWidth, whereby it moves the 2nd field another TabWidth
> to the right. I would like to truncate the first field before it
> exceeds the tab length, but is there any easy way to calculate the
> physical length of a string of characters? I'm guessing not, without
> knowing each characters width.
You can get the width of a string by calling the TListBox.Canvas.TextExtent
method, passing it the string you want to test, then trim down the string if
it's too long. See below for another option though...
> I have a similar problem with a listbox where I want to store a list of
> numbers right-justified. I tried the Format function to force the Width
> but it only works with a fixed size font. I've been forced to use the
> 'precision' option to add leading zeroes to the numbers which looks
> ugly. What is the standard way to do this?
If you want anything non-standard, the best way is to draw it yourself. Set
the TListBox.Style property to lpOnwerDrawFixed and write an event handler
for TListBox.OnDrawItem. This solves both the right-aligned problem and the
item-too-long problem by letting you draw the items exactly how you want
them to look.
Here's a quick sample. You'll have to excuse any errors in coding - I'm a
BCB programmer :>
procedure TForm1.ListBox1DrawItem(Control: TWinControl;
Index: Integer;Rect: TRect; State: TOwnerDrawState);
var
txtSize : TSize;
txt1, txt2 : String;
Rect1, Rect2 : TRect;
begin
{ add some code here to fill txt1 & txt2 with something based on index }
{ now draw it }
Rect1 := Rect;
Rect2 := Rect;
Rect1.Right := Rect1.Left + 50;
Rect2.Left := Rect1.Right;
with ListBox1.Canvas do
begin
Font.Color := clWindowText;
Brush.Color := clWindow;
if odSelected in State then
begin
Font.Color := clHighlightText;
Brush.Color := clHighlight;
end;
if (odGrayed in State) or (odDisabled in State) then
Font.Color := clGrayText;
{ draw first column }
TextRect(Rect1, Rect1.Left + 2, Rect1.Top, txt1)
{ draw second column, right-aligned }
txtSize := TextExtent(txt2);
TextRect(Rect2, Rect2.Left + Rect2.Width - txtSize.cx - 2,
Rect2.Top + 1, txt2);
end;
end;
Hopefully (if I translated it right) that will draw your list items in two
columns, with the first one truncated at column 50 (an arbitrary column
width I snatched out of thin air), and the second one right justified. If
the value for either one is larger than the rect it's drawn in then the
excess will be lost. Only the specified rect will be drawn.
Play around with it and see what happens.
--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur."
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/