whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

77 Visitors Online


 
...show multiline Text in a TCombobox (alternativ)?
Autor: Paul Earley
[ Print tip ]  

Tip Rating (27):  
     




{
  My application needed a ComboBox whose selection choices were too long
  to fit on one line of a ComboBox. I wrote this "quick and dirty" multiline
  ComboBox to fill this need and thought others might want to have this code handy.

  Make sure that the ComboBox Style is set to csOwnerDrawVariable! The two events
  to respond to in csOwnerDrawVariable are MeasureItem and DrawItem.
}

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
var
  
ItemString: string;
  MyRect: TRect;
  MyImage: TImage;
  MyCombo: TComboBox;
begin
  
// Don't waste time with this on Index = -1
  
if (Index > -1) then
  begin
    
MyCombo := TComboBox(Control);
    // Create a temporary canvas to calculate the height
    
MyImage := TImage.Create(MyCombo);
    try
      
MyRect := MyCombo.ClientRect;
      ItemString := MyCombo.Items.Strings[Index];
      MyImage.Canvas.Font := MyCombo.Font;
      // Calc. using this ComboBox's font size
      
Height := DrawText(MyImage.Canvas.Handle, PChar(ItemString),
        - 1, MyRect, DT_CALCRECT or DT_WORDBREAK);
    finally
      
MyImage.Free;
    end;
  end;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  
ItemString: string;
begin
  
TComboBox(Control).Canvas.FillRect(Rect);
  ItemString := TComboBox(Control).Items.Strings[Index];
  DrawText(TComboBox(Control).Canvas.Handle, PChar(ItemString), - 1, Rect, DT_WORDBREAK);
end;


(*** Quote:SDC-Team *** see also http://www.swissdelphicenter.ch/de/showcode.php?id=742  ***)


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners