Fehler in TCustomComboBox

Rund um die LCL und andere Komponenten
Antworten
Benutzeravatar
Lincoln Six Echo
Beiträge: 138
Registriert: Di 26. Aug 2014, 16:42
OS, Lazarus, FPC: Win10, Debian
CPU-Target: I7/I9/Q9650/u.a.
Wohnort: Hamburg

Fehler in TCustomComboBox

Beitrag von Lincoln Six Echo »

Nimmt man eine TComboBox von der Palette und setzt Style auf csOwnerDrawFixed ist es eine nicht editierbare "Klappliste" und man kann die Einträge mit dem Ereignis "OnDrawItem" selbst zeichnen.

Macht man quasi das gleiche in dem man eine eigene TSomethingComboBox = class(TCustomComboBox) erstellt funktioniert weder das OnDrawItem noch die procedure "DrawItem".

Und das unaufgeklappte Control lässt sich dann noch editieren wie TEdit, was nicht sein darf.

Hat jemand das Problem auch schon einmal gehabt und hierfür eine Lösung ?

wp_xyz
Beiträge: 5139
Registriert: Fr 8. Apr 2011, 09:01

Re: Fehler in TCustomComboBox

Beitrag von wp_xyz »

Weiß nicht, was du genau machst, aber bei mir funktioniert's einwandfrei (Das Bearbeiten des nicht ausgeklappten Feldes wird übrigens mit ReadOnly ausgeschaltet):

Code: Alles auswählen

 
unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, types;
 
type
  TMyCombobox = class(TCustomCombobox)
  published
    property Left;
    property Top;
    property Items;
    property Style;
    property OnDrawItem;
 
  end;
 
  { TForm1 }
 
  TForm1 = class(TForm)
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      ARect: TRect; State: TOwnerDrawState);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    ComboBox1: TComboBox;
    MyCombobox: TMyCombobox;
  public
    { public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.lfm}
 
{ TForm1 }
 
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
var
  combo: TCustomCombobox;
begin
  combo := Control as TCustomCombobox;
  combo.Canvas.TextOut(ARect.Left, ARect.Top, combo.Items[Index]);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  Combobox1 := TCombobox.Create(self);
  Combobox1.Parent := self;
  Combobox1.Left := 8;
  Combobox1.Top := 8;
  Combobox1.Items.Add('Item 1');
  Combobox1.Items.Add('Item 2');
  Combobox1.Items.Add('Item 3');
  Combobox1.Style := csOwnerDrawFixed;
  Combobox1.OnDrawItem := @Combobox1DrawItem;
  Combobox1.ReadOnly := true;
 
  MyCombobox := TMyCombobox.Create(self);
  MyCombobox.Parent := Self;
  MyCombobox.Left := Combobox1.Left;
  MyCombobox.Top := Combobox1.Top + 24;
  MyCombobox.Items.Add('ITEM #1');
  MyCombobox.Items.Add('ITEM #2');
  MyCombobox.Items.Add('ITEM #3');
  MyCombobox.Style := csOwnerDrawFixed;
  MyCombobox.OnDrawItem := @Combobox1DrawItem;
  MyCombobox.ReadOnly := true;
end;
 
end.
 

Benutzeravatar
Lincoln Six Echo
Beiträge: 138
Registriert: Di 26. Aug 2014, 16:42
OS, Lazarus, FPC: Win10, Debian
CPU-Target: I7/I9/Q9650/u.a.
Wohnort: Hamburg

Re: Fehler in TCustomComboBox

Beitrag von Lincoln Six Echo »

Yapp ! ReadOnly hatte ich nicht bedacht ! Danke Dir für die schnelle Hilfe :-)

...und lustig : nun klappt auch die überschriebene procedure

procedure TCustomComboBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;

Antworten