Autor Beitrag
AlexII
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 180



BeitragVerfasst: Mo 07.01.08 01:44 
Ich will aus einem StringGrid das gespeicherte Datum laden, es mit dem aktuellen Datum vergleichen und wenn es mehr als 14 Tage sind die entsprechende Zeile grün färben.

Jetzt das Problem: Irgendwie klappt der Code nicht, er will nicht richtig färben und die Schrift befindet sich was weiß ich wo.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
begin
for f:=1 to StringGrid1.RowCount-2 do
begin
DateTimePicker2.Date:=StrToDate(Stringgrid1.cells[5,f]);
DateTimePicker3.Date:=Now;
tage:=Round(DateTimePicker3.Date - DateTimePicker2.Date);
ACol:=0;
ARow:=f;
    if (tage>=14) and (tage<21) then
    begin
    Canvas.Brush.Color:=RGB(0,255,0); //Grün
    Canvas.FillRect(Rect);
    Canvas.Font.Color:=RGB(0,0,0);
  ->  Canvas.TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]); ?????????
    end; 
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 07.01.08 02:06 
Moin!

In welcher Methode steht der Code (wer ruft ihn auf)?

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
AlexII Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 180



BeitragVerfasst: Mo 07.01.08 12:13 
Es steht hier:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin

end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 07.01.08 13:05 
Moin!

Füg´ das mal ein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
begin
for f:=1 to StringGrid1.RowCount-2 do
begin
DateTimePicker2.Date:=StrToDate(Stringgrid1.cells[5,f]);
DateTimePicker3.Date:=Now;
tage:=Round(DateTimePicker3.Date - DateTimePicker2.Date);
ACol:=0;
ARow:=f;
    if (tage>=14) and (tage<21) then
    with StringGrid1 do begin
    Canvas.Brush.Color:=RGB(0,255,0); //Grün
    Canvas.FillRect(Rect);
    Canvas.Font.Color:=RGB(0,0,0);
  ->  Canvas.TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]); ?????????
    end; 
end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 07.01.08 13:43 
Hallo,

@user profile iconNarses: Denke das die Schleife innerhalb OnDrawCell das größere Problem ist.

Das OnDrawCell-Ereignis wird für jede Zelle ausgelöst die gezeichnet werden muss.

In den Variablen Rect, ACol und ARow der Ereignisbehandlungsmethode sind die aktuellen Zellkoordinaten enthalten. Diese aktuelle Zelle muss Dein Code prüfen bzw. zeichnen.

Pseudocode:
ausblenden Quelltext
1:
2:
3:
>> ist aktuelle Spalte = Index 5, dann
>> ist aktuelle Zeile im Bereich der Datum-Zellen, dann
>> prüfe Datum und Zeichne entsprechend


:arrow: Merken:
In OnDrawCell keine Zelle -außer der aktuellen Zelle- beeinflussen, Ausnahme zeichnen über mehrere Zellen.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
AlexII Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 180



BeitragVerfasst: Mo 07.01.08 14:01 
Ich hab's, ich hab's:
Hier wird allerdings nur eine Celle gefärbt, weißt jemand vielleicht wie ich die ganze Zeile färbe?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var f, tage: Integer;
begin
for f:=1 to StringGrid1.RowCount-2 do
begin
DateTimePicker2.Date:=StrToDate(Stringgrid1.cells[5,f]);
DateTimePicker3.Date:=Now;
tage:=Round(DateTimePicker3.Date - DateTimePicker2.Date);

          if (ACol=0) and (ARow=f) and (tage>=14) and (tage<21) then // Spalte 0 und Zeile f färben
          begin
          StringGrid1.Canvas.Brush.Color:=RGB(0,255,0); //Hintergrundfarbe - Grün
          end else if (ACol=0) and (ARow=f) and (tage>=21) and (tage<28) then
              begin
              StringGrid1.Canvas.Brush.Color:=RGB(0,0,255); //Hintergrundfarbe - Blau
              end else if (ACol=0) and (ARow=f) and (tage>=28) then
                  begin
                  StringGrid1.Canvas.Brush.Color:=RGB(255,0,0); //Hintergrundfarbe - Rot
                  end;
          StringGrid1.Canvas.FillRect(rect); // Zelle neu zeichnen
          //StringGrid1.Canvas.Font.Color:=RGB(0,0,0); // Schriftfarbe
          StringGrid1.Canvas.TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]); //Text ausgeben
end;
end;
passiv
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 270

WIN XP Professional
Delphi 7 Enterprise, Java (Eclipse), Python 2.5, Django 1.1
BeitragVerfasst: Mo 07.01.08 15:16 
user profile iconAlexII hat folgendes geschrieben:
...weißt jemand vielleicht wie ich die ganze Zeile färbe?


naja...
meinst du, der Rand der entsteht beim FillRect soll verschwinden?
-> dann: FillRect(Rect(rect.left-1,rect.top-1,rect.width+2,rect.height+2));
ich weiß jetzt nicht, was zuerst kommt, left oder top bzw. width oder height

_________________
Die Probleme, die es in der Welt gibt, sind nicht mit der gleichen Denkweise zu lösen, die sie erzeugt hat.
Albert Einstein
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 07.01.08 15:41 
Hallo,

werf endlich die Schleife weg :motz:
Außerdem schließt du mit if (ACol=0) alle anderen Spalten aus.

Hier mal ein Ansatz, Fehlerbehandlung bzgl. gültiges Datum fehlt noch.
Eventuell musst Du auch noch ein Neuzeichnen des Grids erzwingen, liegt dran wie/wann die Daten in das Grid kommen.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var iTage : Integer;
    s : String;
begin
    with StringGrid1 do
      // auf Acol muss nicht reagiert werden = komplette Zeile färben
      if ARow in [FixedRows..RowCount-2] then //nur die Zeilen färben
        begin
        // Dein Ansatz mit DateTimePicker kann ich nicht nachvollziehen
        iTage := Round(Trunc(now) - StrToDate(Cells[5,ARow]));//<----- Fehlerbehandlung
        if iTage > 13 then
          begin
          case iTage of //case ist übersichtlicher :-)
            14..20 : Canvas.Brush.Color := clAqua;
            21..27 : Canvas.Brush.Color := clYellow;
            else
              Canvas.Brush.Color := clNochNeFarbe;
            end;
          Canvas.FillRect(Rect);
          Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol,ARow]);
          end;
        end;
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
AlexII Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 180



BeitragVerfasst: Mo 07.01.08 20:21 
Ok, hab deine Lösung übernommen :-)