Solved

Delphi Combobox ItemHeight Custom?!

Posted on 2009-04-02
3
3,542 Views
Last Modified: 2013-11-23
Hello,

I'd like to use a Combobox but with a Custom Height...

I tried to change the property "ItemHeight" but doesnt work, except if i set it style to "csOwnerDrawFixed" or "csOwnerDrawVariable" but i dont want to use at this
style, i want to use in normal style "csDropDown" but with a custom ItemHeigth...

Is it possible? :D

Best Regards,
Carlos
0
Comment
Question by:cebasso
[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
  • 2
3 Comments
 
LVL 31

Expert Comment

by:Wayne Barron
ID: 24055979
I do not have Delphi installed right now due to a recent reinstall of the system.
But this is what I found on the subject.

===============================================
The control changes its size when the font size changes.
If you want to change the height without changing the font size,
you need to design your own combobox and override the way the height is set.
===============================================

hth
Carrzkiss
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 24059872
You can also override that using the OnMeasureItem event like in this sample of a Font Combo by Peter Below

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls;
 
const
  UM_MEASUREFONTS = WM_USER;
type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1MeasureItem(Control: TWinControl; Index:
Integer;
      var Height: Integer);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private declarations }
    Procedure UMMeasureFonts( Var msg: TMessage ); message
UM_MEASUREFONTS;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.DFM}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  combobox1.Items := Screen.Fonts;
  combobox1.itemindex := 0;
  // Measure items after form has been shown
  PostMessage( handle, UM_MEASUREFONTS, 0, 0 );
end;
 
procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index:
Integer;
  var Height: Integer);
begin
  // Is called too early to be of use, trying to use the controls
canvas
  // to measure items fails since the control is not visible yet.
  // Return design-time size + some extra spacing.
  Height := (Control As TCombobox).ItemHeight+4;
end;
 
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index:
Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  cb: TCombobox;
begin
  cb:= Control As TCombobox;
  cb.Canvas.FillRect( rect );
  If (index >= 0) and (index < cb.Items.Count) Then Begin
    If odComboBoxEdit In State Then Begin
      // Draw the edit portion of the control, use the controls
      // design-time font for this since the edit control is
      // fixed height and drawing a bunch of symbols if the selected
      // font is Symbol etc. is not very informative for the user.
      cb.Canvas.Font := cb.Font;
      If odSelected In State Then
       cb.Canvas.Font.Color := clHighlightText;
    End
    Else Begin
      cb.Canvas.Font.Name := cb.Items[index];
      cb.Canvas.Font.Size := 10;
    End;
    cb.Canvas.TextRect( Rect, rect.left+2, rect.top+2, cb.Items[index]
);
  End;
end;
 
procedure TForm1.UMMeasureFonts(var msg: TMessage);
var
  i: Integer;
begin
  // use form canvas for measurements
  canvas.font.size := 10;
  For i := 0 To combobox1.items.count - 1 Do Begin
    canvas.font.name := combobox1.items[i];
    combobox1.perform( CB_SETITEMHEIGHT,
                       i,
                       canvas.TextHeight(combobox1.items[i]));
  End; { For }
 
end;
 
end.

Open in new window

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 24059921
Oh, to use csDropDown and still offer a variation of item height, you'd have to subclass TCustomComboBox and override the CNMeasureItem to call MeasureItem if the FStyle included csDropdown and then inherit your combo from that TCustomCombo.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

688 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