Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   ListView OnCustomDrawSubItem (https://www.delphipraxis.net/157471-listview-oncustomdrawsubitem.html)

arest 12. Jan 2011 20:04

ListView OnCustomDrawSubItem
 
hello again,
ich habe eine listview mit einträgen, in der ich prüfen möchte, ob in der 8. spalte text vorhanden ist, und wenn dem so sein sollte, wird diese rot gefärbt... dazu bin ich beim oncustomdrawsubitem ereignis wie folgt vorgegangen:
Delphi-Quellcode:
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
  var i:integer;
begin
  ListView1.DoubleBuffered:=true;
  with Sender.Canvas do
  begin
    Font.Style:=Font.Style-[fsBold];
    Brush.Color:=clWindow;
    Font.Color:=clBlack;
    for i:=0 to form1.ListView1.Items.Count -1 do
    begin
      if (form1.CheckBox1.Checked=true)
      then begin
             if (ListView1.Items.Item[i].SubItems[7] <> '')
             then begin
                    case SubItem of
                      1: Brush.Color:=RGB(88,188,88);
                      2: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      3: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      4: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      5: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      6: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      7: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      8: Brush.Color:=RGB(200,20,20);
                    end;
                  end
             else begin
                    case SubItem of
                      1: Brush.Color:=RGB(88,188,88);
                      2: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      3: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      4: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      5: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      6: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      7: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                      8: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
                    end;
                  end;
           end;
    end;
  end;
end;
die procedure wird ausgeführt, sobald dem listview ein eintrag hinzugefügt/entnommen wird und ich rufe sie manuell über den statuswechsel von der checkbox auf (onclick)! jetzt würde ich erwarten, dass gemäß meiner schleife die gesamte listview durchgegangen wird und für jeden einzelnen eintrag der 7.subitem, sprich die 8. spalte geprüft wird. jedes mal wenn dieses feld leer sein sollte, wird rot gefärbt, ansonsten eben nicht! mein problem ist aber, dass einfach die gesamte 8. spalte rot gefärbt wird... völlig ohne rücksicht auf den jeweiligen eintrag! woran liegt das? :(

thx

Bummi 12. Jan 2011 21:28

AW: ListView OnCustomDrawSubItem
 
Du läuft nicht über die Liste, die Routine wird eh für jedes Item/Subitem aufgerufen, Du mußt nur entscheiden wie Du in dem Fall Malen willst
Delphi-Quellcode:
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
begin
  if Item.SubItems.Count >= SubItem then
    begin
      if Item.SubItems[SubItem-1]='l1_2'
      then Sender.Canvas.Brush.Color := clRed;
      if Item.SubItems[SubItem-1]='L1_1'
      then Sender.Canvas.Brush.Color := clLime;
    end;
end;

Namenloser 13. Jan 2011 06:33

AW: ListView OnCustomDrawSubItem
 
Btw kannst du deinen Code deutlich vereinfachen:
Statt
Delphi-Quellcode:
case SubItem of
  1: Brush.Color:=RGB(88,188,88);
  2: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  3: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  4: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  5: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  6: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  7: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  8: Brush.Color:=RGB(200,20,20);
end;
kannst du schreiben
Delphi-Quellcode:
case SubItem of
  1:   Brush.Color:=RGB(88,188,88);
  2..7: if (item.Index mod 2 = 1) then brush.Color:=RGB(222,222,222) else brush.Color:=RGB(255,255,255);
  8:   Brush.Color:=RGB(200,20,20);
  { würde ich normal anders einrücken, aber die Forensoftware verbockt es }
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:51 Uhr.

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