Delphi-PRAXiS
Seite 3 von 3     123   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi ListBox (https://www.delphipraxis.net/180667-listbox.html)

value is NULL 6. Jun 2014 10:40

AW: ListBox
 
Ich komm nicht dahinter... Aber trotzdem danke für die Hilfe

himitsu 6. Jun 2014 10:54

AW: ListBox
 
Hat eigentlich schon jemand erwähnt, daß der Threadtitel absolut nichts zum Problem aussagt?

Und ja, es gibt schon massig Themen, wo irgendwer irgendwelche Probleme mit seiner ListBox hat.

Jumpy 6. Jun 2014 11:00

AW: ListBox
 
Nur sinngemäß, da ich gerade kein Delphi offen habe, geht bestimmt auch einfacher:

Delphi-Quellcode:
type
  TLBColor=class
    public
     Color:TColor;
  end;


//Item zur Liste hinzufügen:
procedure TForm1.Button1Click(Sender: TObject);
var t:TLBColor;
begin
  t:=TLBColor.Create;
  t.Color:=clWhite;
  Listbox1.AddItem('foo',t);
end;

//in deiner Prüffunktion
while i < Form1.ListBox1.Count do begin
    userId := sqlst.query('select id from user_ where username = '''+Form1.listbox1.Items[i]+'''');
    //user not found
    if userId = '' then //Change Color of current line - cLYellow
      TLBColor(Listbox1.Items.Objects(i)).Color:=clRed;
    i := i + 1;
end;



procedure TForm1.ListBox1DrawItem
    (Control: TWinControl; Index: Integer;
    Rect: TRect; State: TOwnerDrawState) ;
 var
    myColor: TColor;
    myBrush: TBrush;
 begin
    myBrush := TBrush.Create;
    with (Control as TListBox).Canvas do
    begin
      myColor := TLBColor(Listbox1.Items.Objects(index)).Color;
      myBrush.Style := bsSolid;
      myBrush.Color := myColor;
      Windows.FillRect(handle, Rect, myBrush.Handle) ;
      Brush.Style := bsClear;
      TextOut(Rect.Left, Rect.Top,
              (Control as TListBox).Items[Index]) ;
      MyBrush.Free;
    end;
 end;

value is NULL 6. Jun 2014 11:21

AW: ListBox
 
Jetzt hab ichs kapiert.... funktioniert!

Danke an alle für die Geduld!

Popov 6. Jun 2014 13:13

AW: ListBox
 
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:

Zitat von value is NULL (Beitrag 1261557)
Für mich klang das so, als würde dieses Event nur aufgerufen, wenn ich ein neues Item hinzufüge zB Listbox1.Items.Add('WasAuchImmer');

Bei OnDrawIten wird jedes Mal gezeichnet wenn es nötig ist. OnMeasureItem wird nur ein mal ausgeführt, z. B. bei deinem Add.

Zum Code. Den muss man nicht so komplex machen wie hier:
Delphi-Quellcode:
procedure TForm1.ListBox1DrawItem
    (Control: TWinControl; Index: Integer;
    Rect: TRect; State: TOwnerDrawState) ;
 var
    myColor: TColor;
    myBrush: TBrush;
 begin
    myBrush := TBrush.Create;
    with (Control as TListBox).Canvas do
    begin
      myColor := TLBColor(Listbox1.Items.Objects(index)).Color;
      myBrush.Style := bsSolid;
      myBrush.Color := myColor;
      Windows.FillRect(handle, Rect, myBrush.Handle) ;
      Brush.Style := bsClear;
      TextOut(Rect.Left, Rect.Top,
              (Control as TListBox).Items[Index]) ;
      MyBrush.Free;
    end;
 end;
Im Grunde reicht es, was Farbe angeht, das:
Delphi-Quellcode:
 begin
    with (Control as TListBox).Canvas do
    begin
      ...
      Brush.Color := myColor;
      ...
    end;
 end;
Zum empfehlen ist sich an die Listbox ein Objekt mit Infox anzuhängen. Kein großer Aufwand, aber bei OnDrawItem kann man sich die nötigen Infos bezüglich Farbe und weitere Formatierung da rausholen.

Im Anhang habe ich ein kleines Beispiel das zeigt wie das mit dem angehängtem Objekt funktioniert und wie man jeden Item individuell einfärben kann:

Sir Rufo 6. Jun 2014 18:26

AW: ListBox
 
Zitat:

Zitat von Popov (Beitrag 1261609)
Zitat:

Zitat von value is NULL (Beitrag 1261557)
Für mich klang das so, als würde dieses Event nur aufgerufen, wenn ich ein neues Item hinzufüge zB Listbox1.Items.Add('WasAuchImmer');

OnMeasureItem wird nur ein mal ausgeführt, z. B. bei deinem Add.

Bist du dir da sicher?

Popov 6. Jun 2014 18:57

AW: ListBox
 
Nach meiner Kenntnis, Beobachtungen und ausführlichen Tests ja. Wobei mit "einmal" nicht einmal pro ListBox, sondern einmal pro Item, und zwar bei Erstellung des jeweiligen Items gemeint ist.

Wie das in neueren Delphis ist, weiß ich nicht, bei Delphi 7 ist es so. Wenn du andere Kenntnis hast oder Möglichkeiten kennst, her damit.

Luckie 8. Jun 2014 05:01

AW: ListBox
 
Also eigentlich hätte ich jetzt erwartet, das OnMeasureItem bei jedem Zeichen aufgerufen wird, den die Listbox weiß ja vorher nicht, was sie wie Zeichnen soll.


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:22 Uhr.
Seite 3 von 3     123   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz