Autor Beitrag
StrikeLucky
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 35



BeitragVerfasst: Do 28.01.10 20:23 
Moin,

ich habe einige Einträge in einer ListBox, wenn zb der Eintrag xyz auftaucht, soll dieser in roter Schrift dargestellt werden. Kann mir dazu bitte jemand einen Tipp geben?
Danke

LG
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 28.01.10 20:33 
Dafür musst du die Einträge selbst zeichnen. Stichwort: OwnerDraw, schau dir einmal die Ereignisse der ListBox an. ;-)
StrikeLucky Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 35



BeitragVerfasst: Do 28.01.10 20:59 
Danke!
Problem gelöst :)
Holg_i
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Di 09.03.10 10:49 
ich bin ja hier recht neu, wobei ich aber sage, klasse das du das problem gelöst hat.
wäre fein hättest du die lösung hier aufgezeigt damit andere die das problem eventuell auch haben eine lösung finden können.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
     with Control as TListBox do
     begin
           IF Items[Index]='xyz' then Canvas.Brush.Color := clRed else Canvas.Brush.Color := clWhite;
           
           Canvas.FillRect(Rect);
           Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]);
     end;
end;


ich bin immer ganz froh wenn ich auch lösungen finde und nicht nur fragen *grins*
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 09.03.10 10:54 
Und was genau passiert bei dem Quellcode? Oder soll das nur eine Ergänzung sein, und kein Fehlerbericht?

Wobei ich "With" in diesem Zusammenhang (und auch Allgemein) böse finde, da das Formular auch eine Eigenschaft "Canvas" hat.

_________________
PROGRAMMER: A device for converting coffee into software.
Holg_i
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Di 09.03.10 11:10 
Das sollte die Lösung seiner Frage darstellen.

Besser ist also:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
     IF ListBox1.Items[Index]='xyz' then ListBox1.Canvas.Brush.Color := clRed else ListBox1.Canvas.Brush.Color := clWhite;
           
     ListBox1.Canvas.FillRect(Rect);
     ListBox1.Canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
end;
Tranx
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 648
Erhaltene Danke: 85

WIN 2000, WIN XP
D5 Prof
BeitragVerfasst: Sa 11.09.10 06:44 
user profile iconHolg_i hat folgendes geschrieben Zum zitierten Posting springen:
Das sollte die Lösung seiner Frage darstellen.

Besser ist also:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);

begin
     IF ListBox1.Items[Index]='xyz' then ListBox1.Canvas.Brush.Color := clRed else ListBox1.Canvas.Brush.Color := clWhite;
           
     ListBox1.Canvas.FillRect(Rect);
     ListBox1.Canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
end;


Vielleicht ist es besser, falls für mehrere Listboxen eine solche Prozedur benötigt werden, den Parameter Control zu verwenden:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.LB_DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var 
     LB : TListbox;
begin
     if Control is TListbox then begin
        LB := Control as TListbox;
        IF LB.Items[Index]='xyz' then 
            LB.Canvas.Brush.Color := clRed 
        else 
            LB.Canvas.Brush.Color := clWhite;
           
        LB.Canvas.FillRect(Rect);
        LB.Canvas.TextOut(Rect.Left, Rect.Top, LB.Items[Index]);
     end;
end;


Die Prozedur habe ich im Namen daher auch ein wenig abgewandelt. Außerdem funktioniuert das dann auch, wenn Listbox1 umbenannt wird.